[removed] Do I need to put this.var for every variable in an object?

前端 未结 6 1324
太阳男子
太阳男子 2020-11-21 05:06

In C++, the language I\'m most comfortable with, usually one declares an object like this:

class foo
{
public:
    int bar;
    int getBar() { return bar; }
         


        
6条回答
  •  执笔经年
    2020-11-21 05:59

    this is like a public access modifier of objects(variables or functions), while var is the private access modifier

    Example

    var x = {}; 
    x.hello = function(){
        var k = 'Hello World'; 
       this.m = 'Hello JavaScript'; 
    }
    
    var t = new x.hello(); 
    console.log(t.k); //undefined
    console.log(t.m); //Hello JavaScript
    

提交回复
热议问题