Extend setter default object

巧了我就是萌 提交于 2019-12-24 05:26:09

问题


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(parent) {
    var self = this;
    this.enabled = true;
    this.visible = true;
    ...

    this.initialized = false;

    f_createButton(parent, self);

    this.initialized = true;
    ...
}

Button.prototype = {
    get initialized () { 
        return this._initialized; 
    },
    set initialized(bool){ 
        this._initialized = bool
        if(this.initialized === true) {
            ... do default stuff
        }
    },
    get enabled(){
        return this._enabled;
    },
    set enabled(bool){
        this._enabled = bool;
        if(document.getElementById(this.id)) { // is button defined?
            var mClassName = document.getElementById(this.id).children[0].className;
            document.getElementById(this.id).className = (this.enabled === false) ? "button button-Gray_disabled" : "button button-" + this.defaultStyle;
            document.getElementById(this.id).children[0].className = (this.enabled === false) ?  mClassName + "_disabled" : mClassName.replace("_disabled","");
        }
    }
}

function f_createButton("", obj) {
    .... create DOM element 
}

include button.js in html & extend Button "Class/Object":

Object.defineProperty(Button.prototype,"buttonStyle", {
    get : function() {
        return this._buttonStyle;
    },
    set : function(str) {
        this._buttonStyle = str;
        if(this.id !== "undefined" && document.getElementById(this.id)) { // is button defined?
            document.getElementById(this.id).style.backgroundImage = 'url(Images/'+this.buttonStyle+'/buttons.png)';
        }
    }
});

This almost works, but it kills the original Button initialized.

Object.defineProperty(Button.prototype,"initialized", {
    set : function( bool ) {
        this._initialized = bool;
        if(this.initialized === true) {
            this.buttonStyle = "NONE";
        }
    }
});

How can I extend the original setter?


回答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:

// Constructor
function Button() {
    var self = this;
    var _initialized = false; // 'private' property manipulated by the accessors

    this.initialized = false;
    this.createButton();
    this.initialized = true;
}

// Default prototype
Button.prototype = { 
    createButton: function() {
        console.log(' create DOM element ');  
    }  
}

// Default getter/setter on prototype
Object.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() {
        return this._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 accessors
var original = Object.getOwnPropertyDescriptor(Button.prototype, 'initialized');
Object.defineProperty(Button.prototype, "oldInitialized", {
    set : original.set,
    get : original.get
});

// Redefine getter and setter
Object.defineProperty(Button.prototype, "initialized", {
    set : function( bool ) {
        console.log('this is the new setter');
        this.oldInitialized = bool;
    },

    get : function() {
        return this._initialized; 
    }
});

Here is this code at work: http://jsfiddle.net/aTYh3/.



来源:https://stackoverflow.com/questions/15821225/extend-setter-default-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!