Answer the question
In order to leave comments, you need to log in
What is the point of creating a variable through var and not by declaring a property of a global object?
first of all, the question is about browser js
, but also about other-other global objects
, here are two options for creating a property / variable:
var адрес = ""
window.адрес = ""
Answer the question
In order to leave comments, you need to log in
Finally in the furnace variables!
For example, let's display the fibonacci element 40 in the console:
[function(){
this.shift();
this.push(1, 1);
for(;this.length <= this[0]; this.push(this[this.length - 1] + this[this.length - 2]));
console.log(this[this.length - 1]);
}, 40][0]();
in other scopes it is not required.
I have made two rules for myself on this topic. I advise you to stick to them too:
1. If we need to read / write a variable to the GLOBAL scope, we specifically use window .
2. If we need to declare a variable in the CURRENT scope, we use var . Even if we are writing in the global now.
This is code culture. Also, this is especially important in those situations when it is planned to include (with the help of assemblers) one js file into another (into an unknown view).
---------
If you are interested in the technical side of two different records, then in theory - accessing variables without "window" should work a little faster.
window.g = 1;
alert(g);
alert(window.g);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question