2

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?

istepaniuk
  • 4,016
  • 2
  • 32
  • 60
jOte-
  • 51
  • 4
  • the idea is to not change the external button.js... – jOte- Apr 04 '13 at 21:22
  • im, kinda new to this .... PHP is my language... – jOte- Apr 04 '13 at 21:26
  • simple, I want to add some extended features to button.js and catch the initialize event...Maybe i'm wrong an my code suckz? – jOte- Apr 04 '13 at 21:38
  • i see you understand javascript better then me ... is it even possible? – jOte- Apr 04 '13 at 21:42
  • Cleaned up my comments, let's start over. So, what are you trying to do? Create a new class, or modify the original class? Then, when you override a setter method, do you want the original method to run too, or just replace it with the new method? – bfavaretto Apr 05 '13 at 14:10
  • I want the original setter to run to... – jOte- Apr 05 '13 at 16:15

1 Answers1

0

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/.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • rofl u read my thoughts ...http://jsfiddle.net/UqHVX/5/ maybe it will be more clear if u see this one I will check your fiddle – jOte- Apr 05 '13 at 18:05