Propiedades del prototipo de Date JavaScript

Definición y uso

prototype El constructor permite agregar nuevas propiedades y métodos al objeto Date().

En la construcción de propiedades, todos los objetos de fecha recibirán las propiedades y sus valores como valores predeterminados.

En el constructor, todos los objetos de fecha pueden usar este método.

Nota:Date.prototype no hace referencia a un solo objeto de fecha, sino al objeto Date() en sí mismo。

Nota:Prototype es un constructor de objeto global, aplicable a todos los objetos JavaScript。

Ejemplo

Crea un nuevo método de fecha, proporcionando un atributo month-name llamado myProp para el objeto de fecha:

Date.prototype.myMet = function() {
  if (this.getMonth() == 0){this.myProp = "January"};
  if (this.getMonth() == 1){this.myProp = "February"};
  if (this.getMonth() == 2){this.myProp = "March"};
  if (this.getMonth() == 3){this.myProp = "April"};
  if (this.getMonth() == 4){this.myProp = "May"};
  if (this.getMonth() == 5){this.myProp = "June"};
  if (this.getMonth() == 6){this.myProp = "July"};
  if (this.getMonth() == 7){this.myProp = "August"};
  if (this.getMonth() == 8){this.myProp = "September"};
  if (this.getMonth() == 9){this.myProp = "October"};
  if (this.getMonth() == 10){this.myProp = "November"};
  if (this.getMonth() == 11){this.myProp = "December"};
};

Crea un objeto Date y luego llama al método myMet:

var d = new Date();
d.myMet();
var monthname = d.myProp;

Prueba por tu cuenta

Sintaxis

Date.prototype.name = value

Detalles técnicos

Versión de JavaScript: ECMAScript 1

Soporte del navegador

Atributos Chrome IE Firefox Safari Opera
prototype Soporte Soporte Soporte Soporte Soporte

Páginas relacionadas

Tutoriales:Fecha de JavaScript

Tutoriales:Formato de fecha de JavaScript

Tutoriales:Constructor de objetos de JavaScript