Propriedades do prototype de Date JavaScript

Definição e uso

prototype O construtor permite que você adicione novos atributos e métodos ao objeto Date().

No construtor de atributo, todos os objetos Date serão atribuídos atributos e seus valores como valores padrão.

No construtor, todos os objetos Date podem usar este método.

Observação:Date.prototype não se refere a um único objeto de data, mas ao objeto Date() em si.

Observação:Prototype é um construtor de objeto global, aplicável a todos os objetos JavaScript.

Exemplo

Crie um novo método de data, fornecendo um atributo month-name chamado myProp para o objeto de data:

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"};
;

Crie um objeto Date e chame o método myMet:

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

Experimente pessoalmente

Sintaxe

Date.prototype.name = value

Detalhes técnicos

Versão do JavaScript: ECMAScript 1

Suporte do navegador

Atributos Chrome IE Firefox Safari Opera
prototype Suporte Suporte Suporte Suporte Suporte

Páginas relacionadas

Tutorial:Data JavaScript

Tutorial:Formato de data JavaScript

Tutorial:Construtor de objetos JavaScript