I have personally seen that many people who code in JavaScript are those who are good in some other programming language, but because of the requirement (since its the language that browsers understand ) they start coding in javascript.
Most of the time, in such situation, programmers even do not bother to learn this beautiful language. I would like to apologize if anyone felt offended.
But this is the truth. And yes one more truth, there is that a developer can get things done in javascript without knowing much about this language.... may be, it is one of those amazing reasons that I call this language really beautiful....
Anyways what I am going to discuss in this blog is one of the most important thing that one should be aware of before starting to program using javascript : "Global Object".
Well do not confuse"Global Object" with the objects that are created and visible in global scope. Rather here I am talking about the global object which is created when the scripting engine is initialized
It is an intrinsic object whose purpose is to collect global functions and constants (top level functions and variables) into one object. Global Object in javascript is a regular object which we do not create using "new" operator.
When a javascript interpreter starts (browser loads a new page), it creates a new global object and give it an initial set of properties:
We can also add our own properties to this global object.
In top level code i.e the portion of code which is not a part of any javascript function can use "this" to refer to the "global object"
Remember in client side javascript, for all the javascript code contained in a browser, the window object serves as the "global object".
However if our code runs in non browser environment, then we must avoid using "window" to refer to "global object".
In strict mode the way to get reference to the "global object" is to assign a variable in global object referencing itself.
Most of the time, in such situation, programmers even do not bother to learn this beautiful language. I would like to apologize if anyone felt offended.
But this is the truth. And yes one more truth, there is that a developer can get things done in javascript without knowing much about this language.... may be, it is one of those amazing reasons that I call this language really beautiful....
Anyways what I am going to discuss in this blog is one of the most important thing that one should be aware of before starting to program using javascript : "Global Object".
Well do not confuse"Global Object" with the objects that are created and visible in global scope. Rather here I am talking about the global object which is created when the scripting engine is initialized
It is an intrinsic object whose purpose is to collect global functions and constants (top level functions and variables) into one object. Global Object in javascript is a regular object which we do not create using "new" operator.
When a javascript interpreter starts (browser loads a new page), it creates a new global object and give it an initial set of properties:
- global properties like "undefined" ,"Infinity", "NaN"
- global functions like "isNaN()", "parseInt()", "eval()"
- constructor functions like "Date()", "RegExp()", "String()", "Object()" and "Array()"
- global objects like "Math" and "JSON"
We can also add our own properties to this global object.
In top level code i.e the portion of code which is not a part of any javascript function can use "this" to refer to the "global object"
Remember in client side javascript, for all the javascript code contained in a browser, the window object serves as the "global object".
However if our code runs in non browser environment, then we must avoid using "window" to refer to "global object".
In strict mode the way to get reference to the "global object" is to assign a variable in global object referencing itself.
var global= this;
alternatively we can also write a function in global scope and return "this".
function getGlobal() {
return this;
}
return this;
}
So I hope I would have been successful in explaining that what is a "global object" in javascript and when is it created.
All global variables and functions become properties of the "global object". But remember use of global variables and functions are always discouraged.
Before going ahead into the discussion of why global variables and functions are thought of as an evil, I would first like to clarify that what are global variable and function and how do they actually get created in javascript.
Knowingly or unknowingly, in most of javascript code that we write daily, we do create a number of global variables and functions. Suppose your ".js" file contains the following piece of code:
All global variables and functions become properties of the "global object". But remember use of global variables and functions are always discouraged.
Before going ahead into the discussion of why global variables and functions are thought of as an evil, I would first like to clarify that what are global variable and function and how do they actually get created in javascript.
Knowingly or unknowingly, in most of javascript code that we write daily, we do create a number of global variables and functions. Suppose your ".js" file contains the following piece of code:
var variable1="a string",variable2 = false;
function func1() {
if(variable2){
//code goes here
}
}
function func2() {
//code goes here
}
function func1() {
if(variable2){
//code goes here
}
}
function func2() {
//code goes here
}
Then variables variable1, variable2 and functions func1 and func2 are global variables and functions respectively.
To elaborate it even more :
To elaborate it even more :
console.log(window.variable1); // prints "a string" on console
console.log(window.variable2); // prints false on console
console.log(window.func1.toSting()); /* prints function definition on console*/
console.log(window.variable2); // prints false on console
console.log(window.func1.toSting()); /* prints function definition on console*/
i.e you have not just created a variable you have actually added properties to "window" (global) object.
As long as your js file is the only javascript file included in a page, you are safe.
But probably while writing the above piece of code you did not think of the impact- that what might happen, if other than yours js, someone else js is also included in the same page.
Now you may wonder that how does it affect yours code!!! So let me explain you first that every javascript file included in a page runs in the same scope.
In that case if you have global variables or functions in your script, then scripts included after yours that contain the same variable and function name, will overwrite yours.
So now you know that why having global variables and functions in your code are incredible bad idea....
While writing your code you should always keep this in mind that, the more you have globals the more unreliable your code is.Therefore you must try to attain the target of minimizing the globals.
So far so good, but then you may ask that how do I write my code without creating any global variable or function !!!
Well that I will discuss in my next blog....
References:
Microsoft
Awful parts of javascript : O'Reily
As long as your js file is the only javascript file included in a page, you are safe.
But probably while writing the above piece of code you did not think of the impact- that what might happen, if other than yours js, someone else js is also included in the same page.
Now you may wonder that how does it affect yours code!!! So let me explain you first that every javascript file included in a page runs in the same scope.
In that case if you have global variables or functions in your script, then scripts included after yours that contain the same variable and function name, will overwrite yours.
So now you know that why having global variables and functions in your code are incredible bad idea....
While writing your code you should always keep this in mind that, the more you have globals the more unreliable your code is.Therefore you must try to attain the target of minimizing the globals.
So far so good, but then you may ask that how do I write my code without creating any global variable or function !!!
Well that I will discuss in my next blog....
References:
Microsoft
Awful parts of javascript : O'Reily