JavaScript扩展对象和原型
我正在创建一个框架,用原型简化面向对象的编码。但我正在考虑JavaScript中的继承 默认情况下,要扩展对象,我们编写:JavaScript扩展对象和原型,javascript,inheritance,prototype,Javascript,Inheritance,Prototype,我正在创建一个框架,用原型简化面向对象的编码。但我正在考虑JavaScript中的继承 默认情况下,要扩展对象,我们编写: var B = function() { /*...*/ } ; B.prototype = new A() ; 但是构造函数需要参数怎么办 var A = function(args) { if (!args) throw "Arguments required." ; } ; 或者A构造函数也可能在B实例化之前执行不需要的操作 您建议如何替换默认的继承行为?
var B = function() { /*...*/ } ;
B.prototype = new A() ;
但是构造函数需要参数怎么办
var A = function(args) {
if (!args) throw "Arguments required." ;
} ;
或者A构造函数也可能在B实例化之前执行不需要的操作
您建议如何替换默认的继承行为?
(我考虑过在继承或混合时存储所有“类”的所有成员以进行复制。)如果希望从原型继承而不调用构造函数,可以使用
Object.create()
执行以下操作:
var B = function() { /*...*/ };
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
在上面,Object.create(A.prototype)
将返回一个新对象,该对象的原型由A.prototype
给出,它不调用A()
。第二行在那里,因此您可以在任何B实例上查找构造函数属性,它将指向B()
需要注意的一点是,Object.create()
相对较新,因此对于较旧的浏览器,可能需要使用polyfill。您可以在此处找到一个,以及更多信息:
如果要从原型继承而不调用构造函数,可以使用
Object.create()
执行以下操作:
var B = function() { /*...*/ };
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
在上面,Object.create(A.prototype)
将返回一个新对象,该对象的原型由A.prototype
给出,它不调用A()
。第二行在那里,因此您可以在任何B实例上查找构造函数属性,它将指向B()
需要注意的一点是,Object.create()
相对较新,因此对于较旧的浏览器,可能需要使用polyfill。您可以在此处找到一个,以及更多信息:
我通常使用
defclass
实用函数来定义JavaScript中的“类”:
function defclass(base, body) {
var uber = base.prototype;
var prototype = Object.create(uber);
var constructor = (body.call(prototype, uber), prototype.constructor);
constructor.prototype = prototype;
return constructor;
}
然后我使用它如下:
var A = defclass(Object, function () {
this.constructor: function (arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
this.log = function (which) {
console.log(which ? this.arg1 : this.arg2);
};
});
继承非常简单:
var B = defclass(A, function (uber) {
this.constructor = function (arg1, arg2, arg3) {
uber.constructor.call(this, arg1, arg2);
this.arg3 = arg3;
};
this.log = function (which) {
uber.log.call(this, which);
console.log(this.arg3);
};
});
正如您所看到的,当我们扩展一个“类”时,我们使用对象。create
。这是一种新的继承方式。使用new
已经过时了。在B
的构造函数中,我们使用uber.constructor.call将参数传递给A
的构造函数
如果您喜欢这种模式,那么您应该看看这个库。我通常使用defclass
实用函数来定义JavaScript中的“类”:
function defclass(base, body) {
var uber = base.prototype;
var prototype = Object.create(uber);
var constructor = (body.call(prototype, uber), prototype.constructor);
constructor.prototype = prototype;
return constructor;
}
然后我使用它如下:
var A = defclass(Object, function () {
this.constructor: function (arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
this.log = function (which) {
console.log(which ? this.arg1 : this.arg2);
};
});
继承非常简单:
var B = defclass(A, function (uber) {
this.constructor = function (arg1, arg2, arg3) {
uber.constructor.call(this, arg1, arg2);
this.arg3 = arg3;
};
this.log = function (which) {
uber.log.call(this, which);
console.log(this.arg3);
};
});
正如您所看到的,当我们扩展一个“类”时,我们使用对象。create
。这是一种新的继承方式。使用new
已经过时了。在B
的构造函数中,我们使用uber.constructor.call将参数传递给A
的构造函数
如果您喜欢这种模式,那么您应该看看这个库。确实很有趣的库。但是我不喜欢用this.prop=value定义属性和方法代码>到处都是。真是有趣的图书馆。但是我不喜欢用this.prop=value定义属性和方法代码>无处不在。