Javascript 扩展类与 props

Javascript extend class with props?

本文关键字:props 扩展 Javascript      更新时间:2023-09-26

我把类定义为:

function MyClass() {
}
MyClass.prototype = {
       init: function() {
                 alert('My parent class!');
       },
       method1: function() {},
       method2: function() {}
};

和属性对象:

{
     init: function() {
               MySubClass.superclass.init.apply(this, arguments);
               alert('test!');
     },
     test: function() {
               alert();
     }
}

我需要函数,它将使用props(对象)扩展基类(MyClass)并返回新的扩展子类(到MySubClass):

MySubclass = extend(MyClass, {
     init: function() {
               MySubClass.superclass.init.apply(this, arguments);
               alert('test!');
     },
     test: function() {
               alert();
     }
});
构造

函数必须替换为新的构造函数(来自 init)。

我需要一个正确的方法。

为什么效果不好?

extend: function(bc, o) {
        // result class (apply only first constructor)
        var cls = function() {};
        cls.prototype = bc.prototype;
        for (var k in o)
            cls.prototype[k] = o[k];
        cls.superclass = bc.prototype;
        return cls;
    }

你的extend函数必须看起来像这样 - 现在这比你真正实现它的方式要简单得多,但它应该工作:

var extend = function(Super, props) {
   var sinstance = new Super()
   var sclass = props.constructor || sinstance.constructor;
   sclass.prototype.super = sinstance;
   sclass.prototype.constructor = Super;
   //use underscore extend prototypes cause im too lazy to type it out
   _.extend(sclass.prototype, sinstance, props);
   return sclass;
}

调用subclass.super.call(this, props...)允许您访问被覆盖的超级方法。

刚刚测试过,如果页面上有下划线 js,这有效:

function MyClass() {
}
MyClass.prototype = {
       init: function() {
                 alert('My parent class!');
       },
       method1: function() {},
       method2: function() {}
};
MySubclass = extend(MyClass, {
     init: function() {
               this.super.init.apply(this, arguments);
               alert('test!');
     },
     test: function() {
               alert();
     }
});
var test = new MySubclass();
test.init("yo"); //alerts My parent class

对于您的更新,您还可以执行以下操作:

MySubclass2 = extend(MyClass, {
     constructor: function() {
        this.init();
     },
     init: function() {
               this.super.init.apply(this, arguments);
               alert('test!');
     },
     test: function() {
               alert();
     }
});
var test2 = new MySubclass2();//alerts My parent class

如果您愿意使用库,请使用 _.extend from underscore.js。否则,请将此代码添加到代码库中:

extend = function(obj) {
    each(slice.call(arguments, 1), function(source) {
      if (source) {
        for (var prop in source) {
          obj[prop] = source[prop];
        }
      }
    });
    return obj;
  };

我看到你已经用jQuery标记了 - 如果你确实在使用jQuery,你可能对使用$.extend感兴趣。

看看这个: https://github.com/haroldiedema/joii

var BaseClass = function() 
{
    this.some_var = "foobar";
    /**
     * @return string
     */
    this.someMethod = function() {
        return this.some_var;
    }
};
var MyClass = new Class({ extends: BaseClass }, function()
{
    /**
     * @param string value
     */
    this.__construct = function(value)
    {
        this.some_var = value;
    }
})

用法:

var obj = new MyClass("Hello World!");
console.log( obj.someMethod() ); // Hello World!