Javascript Prototype.js和代码中的习惯用法?
我有很多使用原型的代码。看起来像这样Javascript Prototype.js和代码中的习惯用法?,javascript,prototypejs,Javascript,Prototypejs,我有很多使用原型的代码。看起来像这样 var x = Class.create(); Object.extend( Object.extend( x.prototype, Ajax.Application.Base.prototype ), { ... stuff ... } ); 现在Object.extend的签名是Object.extenddestination,source→ 反对 因此,我们正在用Ajax.Application.Base.prototype扩展x.p
var x = Class.create();
Object.extend(
Object.extend( x.prototype, Ajax.Application.Base.prototype ),
{ ... stuff ... }
);
现在Object.extend的签名是Object.extenddestination,source→ 反对
因此,我们正在用Ajax.Application.Base.prototype扩展x.prototype。然后我们将扩展对象x.prototype。。还有更多的东西
这和双延伸相同吗?把它们放在一起有什么好处
这些都是关联的吗
Object.extend(x,y); Object.extend(x,z) == Object.extend(Object.extend(x,y),z);
要回答您的问题-yes Object.extend将返回新对象,该对象包含作为参数传递的两个对象的所有方法和属性 但是,有一种更有用、更简洁的方法来扩展PrototypeJS类 从我的另一个答案中复制一点 这将创建一个从myClass扩展而来的名为myChildClass的类。此方法的区别在于能够使用指向父类的$super方法
var myChildClass = Class.create(myClass,
{
initialize : function($super,option)
{
$super(option);
// the child class needs option's default value at 150 or whatever is
// passed so run the parent initialize first and then redefine the
// option property
this.option = (option ? option : 150);
// you can still run methods in the parent that are not overridden in
// the child
this.testme();
}
});
var child = new myChildClass();
child.showme();
//will alert() 151 and return 151