admin管理员组文章数量:1241085
What's the difference between declaring internal variables inside a JavaScript class with this vs var?
Example:
function Foo( ) {
var tool = 'hammer';
}
function Foo2( ) {
this.tool = 'hammer';
}
One difference we're aware of is Foo2.tool will yield "hammer" whereas Foo.tool will yield undefined.
Are there other differences? Remendations for one vs. the other?
Thanks!
What's the difference between declaring internal variables inside a JavaScript class with this vs var?
Example:
function Foo( ) {
var tool = 'hammer';
}
function Foo2( ) {
this.tool = 'hammer';
}
One difference we're aware of is Foo2.tool will yield "hammer" whereas Foo.tool will yield undefined.
Are there other differences? Remendations for one vs. the other?
Thanks!
Share Improve this question asked Mar 19, 2012 at 5:58 CrashalotCrashalot 34.5k62 gold badges282 silver badges460 bronze badges 4-
Well, obviously the serve different purposes. If you don't have any reason to use
this.tool
, usevar tool
. – Blender Commented Mar 19, 2012 at 6:00 - Thanks, Blender! Would you mind elaborating on the different purposes they serve? – Crashalot Commented Mar 19, 2012 at 6:04
-
Since you can't use
var tool
outside of the class, it isn't made to be used outside of the class.this.tool
is made to be called from outside of the class. – Blender Commented Mar 19, 2012 at 6:04 - 2 phrogz/js/classes/OOPinJS.html – meder omuraliev Commented Mar 19, 2012 at 6:32
1 Answer
Reset to default 17there is no "one or the other" here since the purpose of the two are different.
consider this:
var Melee = function(){
//private property
var tool = 'hammer';
//private method
var attack = function(){
alert('attack!');
};
//public property
this.weapon = 'sword';
//public methods
this.getTool = function(){
return tool; //can get private property tool
};
this.setTool = function(name){
tool = name; //can set private property tool
};
};
var handitem = new Melee();
var decoration = new Melee();
//public
handitem.weapon; //sword
handitem.getTool(); //hammer
handitem.setTool('screwdriver'); //set tool to screwdriver
handitem.getTool(); //is now screwdriver
//private. will yield undefined
handitem.tool;
handitem.attack();
//decoration is totally different from handitem
decoration.getTool(); //hammer
handitem.weapon
in OOP is a "public property", accessible from the outside. if i created this instance ofMelee
, i can access and modifyweapon
since it's open to the public.handitem.tool
is a "private property". it's only accessible from inside the object. it is not visible, not accessible, and not modifiable (at least directly) from the outside. trying to access it will returnundefined
handitem.getTool
is a "public method". since it's on the inside of the object, it has access the private propertytool
and get it for you from the outside. sort of bridge to the private world.handitem.attack
is a private method. like all private stuff, it can only be accessed from the inside. in this example, there is no way to callattack()
(so we are safe from attack :D )
本文标签: htmlDeclaring variables in JavaScript class this vs var DifferenceStack Overflow
版权声明:本文标题:html - Declaring variables in JavaScript class: this vs. var. Difference? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740049880a2222090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论