将JavaScript new关键字与可变长度参数数组一起使用
我正在构建一个函数,允许一个对象被任何其他对象扩展将JavaScript new关键字与可变长度参数数组一起使用,javascript,inheritance,Javascript,Inheritance,我正在构建一个函数,允许一个对象被任何其他对象扩展 Object.prototype.extend = function(constructor, args) { var proto = this; while(proto.__proto__.constructor !== Object) { proto = proto.__proto__ } proto.__proto__ = new constructor(args) console.
Object.prototype.extend = function(constructor, args) {
var proto = this;
while(proto.__proto__.constructor !== Object) {
proto = proto.__proto__
}
proto.__proto__ = new constructor(args)
console.log(this);
}
该方法的调用方式如下:
function ChildModelConstructor(1,2,3) {
this.extend(ParentModel, arguments)
}
or
instanceOfChildModel.extend(ParentModel, [1,2,3])
new constructor(args)
function Model(n) {
this.initParent(arguments)
this.test = n*2;
}
Model.inherit(BaseClass);
问题是如果我这样称呼new:
function ChildModelConstructor(1,2,3) {
this.extend(ParentModel, arguments)
}
or
instanceOfChildModel.extend(ParentModel, [1,2,3])
new constructor(args)
function Model(n) {
this.initParent(arguments)
this.test = n*2;
}
Model.inherit(BaseClass);
父对象的构造函数接收作为arguments对象或数组的arguments
我希望能打电话给你
new constructor.apply(args)
或者类似的,我不想改变这个新的apply的上下文,apply是我知道的使用args对象或数组调用方法的唯一方法
感谢您的帮助:
更新,我找到了一个更好的方法
我提出了一个更好的继承方法,它避免了使用折旧的proto
与我发现的其他继承方案相比,这种方法有几个优点。最大的问题是它不会合并多个层次的原型链。许多方案将子类的proto方法与父类实例变量混合,或者更糟的是,将父类初始化中的所有方法和属性直接放入子类的主体中
缺点是,它是单一继承,并且不能更改单个实例的继承,因为原型属性属于构造函数
Function.prototype.inherit = function(parentClass) {
var newPrototype = Object.create(Object.create(parentClass.prototype));
for(key in this.prototype){
newPrototype[key] = this.prototype[key];
}
this.prototype = newPrototype;
this.prototype.constructor = this;
this.prototype.parentClass = parentClass;
this.prototype.initParent = function(args) {
var proto = Object.getPrototypeOf(Object.getPrototypeOf(this))
this.parentClass.apply(proto, args);
}
this.prototype.uber = function() {
return Object.getPrototypeOf(Object.getPrototypeOf(this));
}
}
您可以这样设置继承:
function ChildModelConstructor(1,2,3) {
this.extend(ParentModel, arguments)
}
or
instanceOfChildModel.extend(ParentModel, [1,2,3])
new constructor(args)
function Model(n) {
this.initParent(arguments)
this.test = n*2;
}
Model.inherit(BaseClass);
下面是JSFIDLE中稍微详细一点的版本 这是未经测试的,但我认为它会起作用。替换:
proto.__proto__ = new constructor(args)
与:
请注意,它已被弃用。这是未经测试的,但我认为它会起作用。替换:
proto.__proto__ = new constructor(args)
与:
请注意,它已被弃用。最好不要将东西附加到对象原型,而只是手动设置继承:
Model function() {
//init parent first because of chromes hidden classes
ParentClass.apply(this, [].slice.call(arguments))
//new instance properties
this.something = 'something'
}
Model.prototype = Object.create(ParentClass.prototype, {
constructor: {value: Model}
})
//Prototype props
Model.prototype.whatever = function(){return 'whatever'}
这还允许您在初始化父类之前修改参数,因为您的新类不应被限制为使用与其父类完全相同的参数最好不要将内容附加到对象原型,而只需手动设置继承:
Model function() {
//init parent first because of chromes hidden classes
ParentClass.apply(this, [].slice.call(arguments))
//new instance properties
this.something = 'something'
}
Model.prototype = Object.create(ParentClass.prototype, {
constructor: {value: Model}
})
//Prototype props
Model.prototype.whatever = function(){return 'whatever'}
这还允许您在初始化父类之前修改args,因为您的新类不应被限制为使用与其父类完全相同的args的可能重复项我认为您是对的,我已开始以不同的方式进行继承,我将编辑/关闭问题我认为您是对的可能重复项,我已经开始用另一种方式进行继承,我将编辑/关闭问题。这将把构造函数生成的键添加到对象之前的proto-chain中的最后一个对象,但不会从构造函数中添加proto-chain。例如,如果您有一个ParentClass.prototype.doSomethin=函数{return 4}的方法;这将在子对象中不可用。这将向对象之前的proto-chain中的最后一个对象添加构造函数生成的键,但不会从构造函数中添加proto-chain。例如,如果您有一个ParentClass.prototype.doSomethin=函数{return 4}的方法;在子对象中将不可用