// inherit methods of Date to extend it.
var extendDate=Date.prototype;
// add year property to Date.prototype, thus to an instance of Date
/*extendDate.year={
get: function() { return this.getFullYear(); },
set: function(y) { this.setFullYear(y); }
};*/
Object.defineProperty(extendDate, "year", {
get: function() { return this.getFullYear(); },
set: function(y) { this.setFullYear(y); }
});
// test year's getter and setter
// first, create an instance of Date
// remember, Date now inherits property year
var now=new Date();
alert(now);
now.year=2000;
alert(now);
En utilisant Object.defineProperty() fonctionne comme prévu, mais pas quand j'utilise
extendDate.year={
get: function() { return this.getFullYear(); },
set: function(y) { this.setFullYear(y); }
};
jsFiddle: https://jsfiddle.net/od53se26/1/
Merci.