'var'vs'this'vs构造函数 - 参数变量 [英] 'var' vs 'this' vs constructor-parameter variables
问题描述
在javascript中给出了这三个构造函数:
In javascript given this three constructor functions:
function Foo(data) {
var _data = data;
}
function Bar(data) {
this.data = data;
}
function Baz(data) {
//just use data freely.
}
除了<$的可见性之外是否有任何区别c $ c>数据施工后会员? (例如,您可以执行新的 Bar()。data
但不能 new Foo()。data
)
Is there any difference aside from the visibility of the data
member after construction ? (e.g. you can do new Bar().data
but not new Foo().data
)
推荐答案
var _data = data;
创建<$的本地副本(不是引用) C $ C>数据。
this.data = data
实际创建了对象本身的属性。
var _data = data;
creates a local copy (not reference) of data
.
this.data = data
actually creates a property of the object itself.
我建议读这个(没有双关语): http://javascriptweblog.wordpress.com/2010/08/30 / understanding-javascripts-this /
I recommend reading this (no pun intended): http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/
这篇关于'var'vs'this'vs构造函数 - 参数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!