最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Declaring variables in JavaScript class: this vs. var. Difference? - Stack Overflow

programmeradmin4浏览0评论

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, use var 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
Add a ment  | 

1 Answer 1

Reset to default 17

there 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 of Melee, i can access and modify weapon 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 return undefined

  • handitem.getTool is a "public method". since it's on the inside of the object, it has access the private property tool 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 call attack() (so we are safe from attack :D )

发布评论

评论列表(0)

  1. 暂无评论