declare var variable versus using self [duplicate] - javascript

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.

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).

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.

Javascript: set object variable to inner object instance [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 6 years ago.
I'm trying to create a class to better organize my code, I decided to go the object literal route
var MyClass = {
}
I'm currently running into issues migrating some of my functionality though, previously I had global scope variables Channels for example set to an object instance. Is there a way to still do this in javascript within a class without moving the object into global scope?
var Prime = {
Channel: new __Prime__Channel(),
//The object in question
__Prime__Channel: function() {
this.Property = Value;
},
}
this throws
Undefined reference __Prime__Channel() at line 2
In global scope you could do
var Channel = new __Prime__Channel();
function __Prime__Channel() {
this.Property = Value;
}
without any errors
An object is no class, please ensure you're using the right terminology.
With an object literal it is not possible to do what you want, the only way is
var Prime = {
__Prime__Channel: function() {
this.Property = Value;
}
}
Prime.Channel = new Prime.__Prime__Channel();
However, maybe an object literal is the wrong pattern anyway. If you want to avoid global variables, look into the module pattern and IIFEs.

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.

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

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.

Categories

Resources