Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've modified John Resig simple javascript inheritance framework, by adding set function with setter functionality. It seems that it works. Is it written correctly? Can you see some undesired behavior? Framework:

(function(){
   var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
   this.Class = function(){};
   Class.extend = function(prop) {
     var _super = this.prototype;
    initializing = true;
    var prototype = new this();
    initializing = false;
    for (var name in prop) {
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
            this._super = _super[name];
            var ret = fn.apply(this, arguments);       
            // CHANGED HERE FROM this._super = tmp;
            // don't like some not used properties 
            if(tmp)this._super = tmp;
            // END OF CHANGE
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
    function Class() {
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
    Class.prototype = prototype;
    Class.prototype.constructor = Class;
    Class.extend = arguments.callee;

    // CHANGED HERE
    // Set method
    Class.prototype.set = function(attrs) {
      for (var attr in attrs) {
        //if exist setter function this.on_change{key of wrap: function(key of wrap, new value)
        if(this.on_change[attr])this.on_change[attr].call(this,attr,attrs[attr]);
        else this[attr] = attrs[attr];
      }
      return this;
    };
    if(!Class.prototype.on_change)Class.prototype.on_change={};
    // END OF CHANGE

    return Class;
  };
})();

Test code:

var Person = Class.extend({
  a: 15,
  b: 30,
  init: function(){
      //...
  },
  on_change:{
      b: function(oldKey, set){this[oldKey] = set*2}
  }
});
var p = new Person();
p.set({a:25,b:40});
share|improve this question
Code like that is an anti pattern. It should be avoided by removing it. – Raynos Dec 9 '11 at 6:12
Can you be please more specific. Very similar approach you can see in prototype.js or backbone.js. Extendable is also jquery object. Maybe there is more coupling on "on_change" object, but setter are placed inside the object. Code is dry and readable in my opinion. – Tal Dec 10 '11 at 11:01
I mean using setters and adding listeners to them. It's an anti pattern and should be avoided. There are a view use-cases for reactive programming but those are edge cases. – Raynos Dec 10 '11 at 12:49

migrated from stackoverflow.com Dec 7 '11 at 13:51

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.