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.
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 { } .
Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not.
11.4.You can't declare the same variable twice at the same level: assert.
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
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With