Javascript: this.varName = "" vs var varName = "" [duplicate] - javascript

This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?

It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.

var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.

Related

What is the scope of my variable when I use let? [duplicate]

This question already has answers here:
Do let statements create properties on the global object?
(5 answers)
A javascript 'let' global variable is not a property of 'window' unlike a global 'var' [duplicate]
(1 answer)
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Im new in javascript and in programming.
If I declare a variable with var I see that my variable is declared globally and my variable is inside the window object.
Example:
var element1 = 1;
window.element1; //This returns 1
But when I use let I can't access my variable using the window object.
Example:
let element2 = 1;
window.element2; //This returns undefined
So, where has been my variable element2 been declared?
What is the scope of element2 ?
You are confusing scope and automatic attachment to the default object.
If you use let or var outside of any block, function, or module then the scope will be global.
var will also attach a property of the same name to the default object (which is window in the case of JS running in a browser).

Why global let variables not added to window object of browser? [duplicate]

This question already has answers here:
why don't const and let statements get defined on the window object [duplicate]
(2 answers)
Closed 3 years ago.
Consider the code below which I am running in my chrome browser console:
var x = "hello";
let foo = function(){
console.log("hi");
};
console.log(x); // hello
console.log(foo()); //hi
console.log(window.x); // hello
console.log(window.foo()); // Error
If I had used var foo =... instead of let foo = .. then this would have worked. Why ?
This is by design:
let allows you to declare variables that are limited to a scope of a block statement, or expression on which it is used, unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it (see below).
Just like const the let does not create properties of the window
object when declared globally (in the top-most scope).

Access local variable from the global context? [duplicate]

This question already has answers here:
What is a 'Closure'?
(22 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
function wrapValue(n) {
var localVariable = n;
return function() { return localVariable; };
}
var wrap1 = wrapValue(1);
var wrap2 = wrapValue(2);
console.log(wrap1());
// → 1
console.log(wrap2());
// → 2
In this example how is it allowed to access localVariable, which is local to wrapValue() function, from the global environment?
The book, included this example, stated the reason as follows:
"multiple instances of the variable can be alive at the same time".
But I didn't understand.
Your function sets a private value (localVariable) when called and returns it immediately so you can assign it to a another variable without change the private var inside function.

I want to know why we use var for all types of data [duplicate]

This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 6 years ago.
What is difference between declaring variable using var and without var?
a=2;
b=2;
if(a==b)//returning false
if(a===b)//returning false
var a=2;
var b=2;
if(a==b)//returning true
if(a===b)//returning true
Why?
Without var will declare the variable globally. Using var will declare the variable locally in the current scope.
var defines variable globally, Means that variable defined with var can be accessible form any script below it. And without var it cant
We use var for all types because since JavaScript has dynamic typing the type can be inferred automatically during run-time.
Now, for the difference between using the var or not, if you're in a function then var will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope at which point it will create it.

declare var variable versus using self [duplicate]

This question already has answers here:
Javascript: Do I need to put this.var for every variable in an object?
(6 answers)
Closed 8 years ago.
A bit of confusion. Here's the JS code:
(function(){
var self = this;
var view = 12;
self.value = null;
....
});
I understand using var view = 12 creates a local scope for the view variable. But doesn't self.value also create a "local" scope for the value variable? If so, what is the difference or am I missing something?
self is a local variable. With self.value, you're adding a prototypical property on to the self variable and assigning it to null.

Categories

Resources