JS: local var X global (window) var [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
window.variableName
(6 answers)
Closed 4 years ago.
Studying global vars in JS here, I set out to try it and to my surprise this:
var thisVar = "global var";
function showVarLet() {
var thisVar = "local var";
console.log("%s %s", thisVar, window.thisVar);
}
showVarLet();
gives me:
local var
undefined
but the same in the browser console, gives me:
local var
global var
So, what´s with this window object?
EDIT:
I tried to check in the console what would happen if instead of window.thisVar I referenced this.thisVar, my assumption was that I would access the local variable but I keep accessing the global one, why so?

and the code I showed is in a function called global()
then none of the two thisVars is global, one is a local variable of the global() function, the other one is a local variable of showVarLet(). You cannot access local variables via window..

Related

Javascript object creation practices [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
This is what I think is best for making an object in js
function obj(){
var x = "hi";
this.getX(){return x;}
}
var y = new obj()
console.log(y.x); //this returns undefined
But from what I have seen, using this.variable is used more often in object creation.
I am thinking in java where things should be "private" in a class (note I have read about closures), does that apply in js?
What is considered the best way of object creation?
It sounds like you're trying to apply Java concepts to JavaScript, but things works completely differently in JS. You should check out the MDN article on JavaScript closures.
var variables exist in the closure. They are accessible from any function declared in the same scope.
var me = 'hello';
function someFunction() {
console.log(me);
}
someFunction(); //prints 'hello' to console
this variables will be directly accessible, even outside the scope, in the resulting object.
function someFunction() {
this.me = 'hello';
}
var instance = new someFunction();
console.log(instance.me); //prints 'hello' to console

Create variable name based on argument sent to function in javascript [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I wanted to create variable name based on value sent to function in javascript.
like following, when i call function variable : variable("zebra"); this should return variable name as zebra1
function create variable(i){
return i+"1";
}
var variable("zebra")="abc";//this line should create variable zebra1 and initialise as abc
Try:
window['zebra'] = 'abc';
The window object holds all the global variables, assuming this is a request for global variables.
To specifically answer your question, you could put return window[i + '1'] = 'abc'; in your variable naming function.
Alternatively, you could create a global (or local) object named variables to hold all your variables:
function whoknows() {
var variables = {};
variables['zebra'] = 'abc';
}
Read more on working with objects at mozilla.org
You can create global variable with
window["zebra"] = "abc";
and use later ether with the same indexer syntax or directly - zebra.

Equivalent of window["myString"] for Local Variable [duplicate]

This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
Let say I got a string with the name of the variable I want to create.
If I want to create it with a global scope, I'd use window["myString"] = 100; or global["myString"] = 100; on Nodejs.
Is there a way to do the same but create the variable with local scope? (Ex: Inside a function)
EDIT: Note: The goal is to access the variable with its name directly. Ex: myString
I already know that I could easily create a object that would have the value as an attribute. Ex: obj.myString. But this is not what I'm looking for.
The local scope is not exposed as the global object is.
However...
function someFunction() {
var locals = {};
locals['someLocalVar'] = 'local var';
}

Where are globals stored in Node.JS? Is there a window-like object in Node? [duplicate]

This question already has an answer here:
What is the node.js equivalent of window["myvar"] = value?
(1 answer)
Closed 8 years ago.
If I write:
myVar = 123; // note the missing var keyword
is there an object in NodeJs where I can retrieve this variable from?
In a browser this would be:
window.myVar
Node's globals are stored in the global variable and is documented here. Since "global" variables are local to each module, global is the true global variable that is shared across modules.
They are stored in the global object

why without var? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between using var and not using var in JavaScript
seems stupid question but just curious.
num = "hello";
alert(num);
why is this possible I didnt initialize the num variable here.
num = "hello";
instead of
var num = "hello";
var means "Scope this variable to this function" not "Make it possible to use this variable".
If you leave var off then it will use the variable in the next scope up from the function. If that scope doesn't have the var yourVar then it will keep going up the scope chain until it becomes a global and dangles off the window object (assuming a browser or another JS environment in which the default object is window).
(This changes in strict mode, which you are presumably not using.)
Without the var keyword the variable is created at the global scope (in browsers under window).
If you add the var you just assign the variable to the current scope, which in most cases will be a function scope.
In general it will be better to use a function scope in order to not pollute the global scope and prevent conflicts between multiple variables of the same name!

Categories