Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 扩展setter默认对象_Javascript_This_Defineproperty - Fatal编程技术网

Javascript 扩展setter默认对象

Javascript 扩展setter默认对象,javascript,this,defineproperty,Javascript,This,Defineproperty,就像你们都知道的按钮是一个按钮。。。点击,向上,向下,做这个,做那个。 所以我写了一些默认的按钮行为“类/对象” 外部默认按钮.js: function Button(parent) { var self = this; this.enabled = true; this.visible = true; ... this.initialized = false; f_createButton(parent, self); this.i

就像你们都知道的按钮是一个按钮。。。点击,向上,向下,做这个,做那个。 所以我写了一些默认的按钮行为“类/对象”

外部默认按钮.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,"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
});
如果我理解您的要求,那么您希望重新定义
初始化的
访问器(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; 
    }
});

下面是工作中的代码:。

我们的想法是不要更改外部按钮。js…im,这有点新。。。。PHP是我的语言…很简单,我想向button.js添加一些扩展功能并捕获初始化事件…也许我错了,我的代码很烂?我看你比我更了解javascript。。。有可能吗?清理我的评论,让我们重新开始。那么,你想做什么?创建新类,还是修改原始类?那么,当你重写一个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; 
    }
});