HierHinzufügen Eigenschaft Object.prototype Punktnotation nicht wie erwartet
// 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);
Object.defineProperty Verwendung() funktioniert wie erwartet, aber nicht, wenn ich
extendDate.year={
get: function() { return this.getFullYear(); },
set: function(y) { this.setFullYear(y); }
};
JSFiddle verwenden: https://jsfiddle.net/od53se26/1/
Danke.