OOP JS 中的 this.constructor.SomeVarible 与 this.SomeVarible
那么下面两个例子有什么区别呢?
var SomeFn = function (name){this.constructor.SomeVarible = name}
和
var SomeFn = function (name){this.SomeVarible = name}
So what is the difference between the following 2 examples?
var SomeFn = function (name){this.constructor.SomeVarible = name}
And
var SomeFn = function (name){this.SomeVarible = name}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您
在第一种情况下,该值将被分配给
SomeFn.SomeVarible
,因为this.constructor
引用SomeFn
。实际上我不明白为什么有人会做这样的事情,但如果有人有想法,请告诉我。在第二种情况下,它将被分配给
obj.SomeVariable
,因为this
引用新创建的对象并被分配给obj
。Assuming you have
In the first case, the value will be assigned to
SomeFn.SomeVarible
, sincethis.constructor
refers toSomeFn
. I actually don't see a reason why one would do something like this, but if anyone has an idea, please let me know.In the second case, it will be assigned to
obj.SomeVariable
, sincethis
refers to the newly created object and is assigned toobj
.