JavaScript - 尝试从原型中添加原型
这听起来比应有的容易,但我在尝试解决此问题时遇到了问题。我的情况基本上是,我可以创建一个将使用原型的类(例如:function exClass() {}
)。
如果我想添加到类中,我可以使用:exClass.prototype.newMethod() = '';
。
那么为什么如果我处于原型“newMethod”中,我就无法再向“exClass”添加新原型了。我的意思的一个例子是: this.prototype.test_var1 = ''
- 它失败了,exClass.test_var1
也是如此。
为什么我无法从某个子类中向该类添加更多内容?
This sounds easier than it should be, but I'm having issues trying to resolve this. My situation is basically, I can create a class that's going to use prototype (example: function exClass() {}
).
And if I want to add onto the class, I can just use: exClass.prototype.newMethod() = '';
.
So why is it that if I'm in, for example, the prototype "newMethod", I can't add a new prototype to "exClass" anymore. An example of what I mean is: this.prototype.test_var1 = ''
- it fails, as does exClass.test_var1
.
Why am I unable to add more to the class from within one of its subclasses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对象的原型不是对象上称为
prototype
的属性。函数上的原型字段是将成为使用该函数创建的对象的原型的对象。对象可以通过构造函数访问创建它的函数的原型。例如,this.constructor.prototype.test_var1 = ''
在大多数情况下都有效。我之所以说在大多数情况下,是因为许多 JavaScript 引擎都有一个内置的 __proto__ ,它是对象的原型,可以动态修改,但例如 IE 不支持这一点。
在 ES5 中,您可以使用 Object.getPrototypeOf() 可靠地获取原型。例如,您可以在 ES5 中使用
Object.getPrototypeOf(this).test_var1 = ''
,它可以在现代浏览器中工作,但不能在不支持 ES5 的浏览器中工作。The prototype of an object is not a property called
prototype
on the object. The prototype field on a function is the object that will become the prototype of objects created with that function. An object can access the prototype of the function that created it through theconstructor
function. For example,this.constructor.prototype.test_var1 = ''
would work in most cases.I say in most cases because many JavaScript engines have a built in
__proto__
that is the object's prototype and can be modified on the fly, but this is not supported in IE for example.In ES5 you can use Object.getPrototypeOf() to get the prototype reliably. So for example, you can say,
Object.getPrototypeOf(this).test_var1 = ''
in ES5 which will work in modern browsers but not browsers without ES5 support.构造函数
exClass
的prototype
属性与实例的prototype
属性引用的对象不同exClass
的,这是this
在newMethod
中引用的内容。证明:输出:
更一般地,JavaScript 中的每个对象都有一个原型 object。函数的
原型
属性指定使用该函数创建的对象类的原型对象。没有什么可以阻止您从另一个函数中修改函数的原型:
或者更一般地说:
但我不推荐它。
The
prototype
property of the constructor functionexClass
does not refer to the same object as theprototype
property of an instance ofexClass
, which is whatthis
references insidenewMethod
. Proof:Output:
More generally, every object in JavaScript has a prototype object. The
prototype
property of a function specifies what the prototype object will be for the class of objects created with that function.Nothing prevents you from modifying a function's
prototype
from within another function:Or more generically:
But I wouldn't recommend it.
您无法通过
this.prototype
获取对象的父原型。您必须使用this.constructor.prototype
(尽管这会影响该类型的所有对象的行为,在本例中是exClass
)。该片段将提醒“hello world”。You can't get at an object's parent prototype via
this.prototype
. You have to usethis.constructor.prototype
(although this will affect the behavior of all objects of that type, in this caseexClass
). This snippet will alert 'hello world'.