Extend Setter Default Object
Like you all know a button is a button... click, up, down, do this, do that. So I wrote some default button behavior 'class/object'. external default button.js: function Button(par
Solution 1:
What you're asking seems unusual, but let's try. First of all, consider this base class, which would be in your external js file:
// ConstructorfunctionButton() {
var self = this;
var _initialized = false; // 'private' property manipulated by the accessorsthis.initialized = false;
this.createButton();
this.initialized = true;
}
// Default prototypeButton.prototype = {
createButton: function() {
console.log(' create DOM element ');
}
}
// Default getter/setter on prototypeObject.defineProperty(Button.prototype,"initialized", {
set : function( bool ) {
console.log('this is the original setter');
this._initialized = bool;
if(this._initialized === true) {
console.log('do default stuff')
}
},
get : function() {
returnthis._initialized;
},
configurable : true// IMPORTANT: this will allow us to redefine it
});
If I understand what you're asking, you want to redefine the initialized
accessor (getter/setter), but still have a reference to the old one. Maybe there's a better way to do that, but you could copy the original accessor into a new one, then redefine it:
// Keep reference to the old accessorsvar original = Object.getOwnPropertyDescriptor(Button.prototype, 'initialized');
Object.defineProperty(Button.prototype, "oldInitialized", {
set : original.set,
get : original.get
});
// Redefine getter and setterObject.defineProperty(Button.prototype, "initialized", {
set : function( bool ) {
console.log('this is the new setter');
this.oldInitialized = bool;
},
get : function() {
returnthis._initialized;
}
});
Here is this code at work: http://jsfiddle.net/aTYh3/.
Post a Comment for "Extend Setter Default Object"