我正在构建一个函数,用一些常用函数翻新我的一些原型。是否可以向原型添加一个不共享的变量?
我也想通过这个修车添加对象实例的具体变量,有点儿像:
function give_weird_container(target) {
target.<somehow instance specific, not prototype>.underlying_container = [];
target.prototype.container_func = function(x, y, z) {
return this.underlying_container[x + 2*y + 3*z];
}
}
function my_class1() {}
give_weird_container(my_class1);
,现在当我创建my_class1的新实例,它应该有一个属性“uderlying_container”,将采取行动相同的,如果我在构造函数称为
this.underlying_container = [];
。
在give_weird_container函数的限制范围内,这可能吗?
:你是说? 'target.underlying_container = [];' –
'不知何故特定实例'但函数中没有实例? – bugwheels94
@ ankitbug94好吧,是的,该属性将被添加到所有新实例 – user81993