javascript - 扩展 setter 默认对象

标签 javascript this defineproperty

众所周知,按钮就是一个按钮...单击、向上、向下、做这个、做那个。 所以我写了一些默认按钮行为“类/对象”。

外部默认 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 
}

在 html 中包含 button.js 并扩展按钮“类/对象”:

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)';
        }
    }
});

这几乎可以工作,但它会杀死初始化的原始 Button。

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

如何扩展原始的 setter?

最佳答案

您的要求似乎很不寻常,但让我们试试吧。首先,考虑这个基类,它会在你的外部 js 文件中:

// 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
});

如果我理解你的问题,你想重新定义 initialized 访问器(getter/setter),但仍然有对旧访问器的引用。也许有更好的方法来做到这一点,但您可以将原始访问器复制到一个新访问器中,然后重新定义它:

// 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; 
    }
});

这是工作中的代码:http://jsfiddle.net/aTYh3/ .

关于javascript - 扩展 setter 默认对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821225/

相关文章:

javascript - 重新加载页面后喜欢喜欢简历

javascript - 在 Rails 应用程序中隐藏标签云

javascript - jquery this 引用 if 语句中的类

javascript - "this"关键字与多个对象

javascript - 如何撤消 Object.defineProperty 调用?

javascript - 取消设置并为使用 DefineProperty 定义的属性设置新值

javascript - 在Javascript中拆分日期

javascript - Cufon渲染换行符

javascript - 对 "softBind"函数如何工作的困惑

javascript - javascript中如何防止对象属性不被扩展