JavaScript Date prototype eigenschappen

Definitie en gebruik

prototype De constructor maakt het mogelijk om nieuwe eigenschappen en methoden toe te voegen aan het Date()-object.

Bij het aanmaken van eigenschappen worden alle datumobjecten toegewezen aan eigenschappen en waarden als standaardwaarden.

Bij het aanmaken van de constructor kunnen alle datumobjecten deze methode gebruiken.

Note:Date.prototype does not refer to a single date object, but to the Date() object itself.

Note:Prototype is a global object constructor that applies to all JavaScript objects.

Example

Create a new date method that provides a month-name property named myProp for the date object:

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

Create a Date object and then call the myMet method:

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

Try it yourself

Syntax

Date.prototype.name = value

Technical details

JavaScript version: ECMAScript 1

Browser support

Properties Chrome IE Firefox Safari Opera
prototype Support Support Support Support Support

Related pages

Tutorial:JavaScript date

Tutorial:JavaScript date format

Tutorial:JavaScript object constructor