What is THE Javascript Global Object? Take in consideration the following code:
var a = 1; b=2;Here we declare 2 variables , but what's the difference. Well, in terms of usability the difference is that b is declared as a property of the global Object 'window' (in the browser) and so any javascript code can access this name (unless explicitly overriden).
So here is the first javascript mistery, So, how can I know, (as a javascript programmer), what is this Global Context Object ? For example, I need this object for performing some global evaluation and for that I need this Global Context object. The answer is simple:
`this` outside any function will be the global object.
...
Explained in other words, this is my way of understanding The Javascript Global Object:
Every code must be runned inside a function, even that code for which we don't see its corresponding function, like when we use eval() or the top-most code that is not inside any function. All the other javascript code resides on a function body. For the top most javascript written code we
the chat conversation in irc.freenode.org#javascript that teach me :
(Cancerbero_sgx): hi all. question: when I assign a value to a name without the 'var' keyword, like global1=1, those variables are stored as properties of some global object, for example, in a browser on the 'window' object. Does that depends on the implementation?, or has the javascript language some reference this object ? example: for(var v in StandarGlobalObject){...} ? thanks in advance
....
Well, an old javascripter that admit it is magic related to this issue. It is a mystery indeed.
But immediately we we start to suspect a relationship between the keywords var, this and with.
The first to notice is that, when declaring a named value, if we omit the keyword 'var' (when declaring a variable) or if we omit the keyword 'this' when declaring an attribute then the name is assigned as a property to the global object, in the case of browsers the window. The following code try to show what i'm talking about:
<script> // the Global Object. Because we are in the top-most context (no function sourrounding us) even if we are declared with 'var' we are global variables (properties of the current global object 'window'). This is an special case! var GLOBALOBJECT=this; var Class1 = function(){ this.attribute1="hello"; nonattribute1="world"; //missing 'this' so this is a global veriable, this is a property of GLOBALOBJECT var DeeperContext = function() { var var1="just a veriable"; global2="missing var so i'm global"; this.attribute2="instance attribute"; nonattribute2="missing this so i'm global"; // notice that DeeperContext constructor is executed on a second level context, but nonattribute2 name is assigned to the GLOBALOBJECT, not the parent context as some may expect. } this.attribute12 = new DeeperContext(); } var c1 = new Class1(); console.log(GLOBALOBJECT); //at this point the GLOBALOBJECT contains 2 new properties nonattribute1 and nonattribute2 </script>
No hay comentarios:
Publicar un comentario