Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript var vs this [duplicate]

If my code looks like this, what is the preferred method of using var vs this?

function MyObject() {
    var self = this;

    var a = 1;
    this.b = 2;

    var innerMethod = function() {

        logMessage(a); 
        logMessage(self.b); 
    }
}

As I understand, var will survive as long as MyObject lives, so isnt this the same as using this?

EDIT:

To clarify the question more, I am ONLY interested in accessing variables from inside the object, not from outside.

like image 487
Toniq Avatar asked Dec 21 '14 12:12

Toniq


People also ask

Why VAR should not be used in JavaScript?

Scoping — the main reason to avoid var var variables are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } .

Can we duplicate a variable name using VAR and let keyword?

Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not.

Can I declare a variable twice in JavaScript?

11.4.You can't declare the same variable twice at the same level: assert.


2 Answers

with var a you cant access the variable outside the scope however assigning with this can be accessed when you make an object

We add properties to this when we want the properties to exist with the life of the object.We Use var for local variables.

"I am ONLY interested in accessing variables from inside the object, not from outside."

Answer to this statement is use var if you want to use only inside the function not outside as variable defined with var are accessible only to code in the scope in which they were declared, or in lexically nested scopes.

so as Shomz suggested you can check it as:

var o = new MyObject();

a will be undefined as it is defined with var

  o.a; // undefined

while b will be return 2 as it is on this

 o.b; // 2
like image 184
A.B Avatar answered Oct 16 '22 00:10

A.B


If you define a variable as var, then its scope is limited to the function (it can't be seen from the outside). In OO terms it's somehow like a private property, without actually being a property at all.

If you define a variable as a property (this.name) then it is accessible from the outside.

Same goes for functions. Functions declared inside the scope of the function, but not assigned to a property, are only visible from the inside. If you assign a function to a property you are able to access that function from the outside (as long as the property keeps pointing to that function).

function Person(){

    // Declared variable, function scope
    var name = "John";          

    // Property        
    this.surname = "Doe";       

    // Assign anonymous function to property
    this.getName = function(){  
        return name;
    }

    // Assign anonymous function to property
    this.getSurname = function(){ 
        return this.surname;
    }

    // Declare function
    function saluteCasually(){      
        console.log("Hi folks!");
    }

    // Declare function
    function salutePolitely(){          
        console.log("Nice to meet you");
    }

    // Assign (not anonymous) function to property
    this.salutePolitely = salutePolitely;

}

var person = new Person();

console.log(person.name);           // undefined
console.log(person.getName());      // "John"
console.log(person.surname);        // "Doe"
console.log(person.getSurname());   // "Doe"

person.saluteCasually(); // Error: person has not a property "saluteCasually".
person.salutePolitely(); // Prints "Nice to meet you";

person.salutePolitely = function(){ // Properties can be messed with from anywhere!
    console.log("Bananas");
}
person.salutePolitely(); // Prints "Bananas";
like image 21
abl Avatar answered Oct 15 '22 23:10

abl