JavaScript Date prototype atributo

Paglilinaw at paggamit

prototype Ang constructor ay nagbibigay sa iyo ng kakayahan na magdagdag ng bagong atributo at paraan sa obyekto ng Date().

Sa pagkakalatag ng atributo, ang lahat ng mga obyekto ng petsa ay bibigyan ng atributo at halaga bilang default na halaga.

Sa pagkakalatag ng constructor, ang lahat ng mga obyekto ng petsa ay maaaring gamitin ang paraan ito.

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.

Instance

Create a new date method, providing 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

属性 Chrome IE Firefox Safari Opera
prototype 支持 支持 支持 支持 支持

相关页面

教程:JavaScript 日期

教程:JavaScript 日期格式

教程:JavaScript 对象构造器