The global object is very important in Javascript. It contains certain properties that are widely used – remember that in Javascript, the members of an object are typically referred to as “properties”. These properties include certain functions and even other objects, and are all available to any given Javascript program.
The global object is created as soon as the Javascript interpreter starts. For Javascript running inside a browser, this means that as soon as a new page is loaded by the browser, the global object will be created.
When the global object is created, it is given some initial properties. These properties include:
global functions such as parseFloat(), and eval()global properties such as NaN and Inifinityglobal objects such as Intl and Math
Top-level Javascript code is Javascript code that is not part of a function – in other words any Javascript code that is outside of a function is considered to be top level code.
You can actually reference the global object in top-level Javascript code. In order to do that, you would use the Javascript keywordthis. Here is what it would look like:
//the code below uses an arbitrary variable name - globalObj -//to reference the global objectvar globalObj = this;
If you have ever dealt with client side Javascript – and by client side Javascript we just mean Javascript used by a web browser – then you have probably heard of the Window object. The Window object also acts as a global object for any and all of the Javascript code within a browser window. Remember that the Window object is basically a representation of an individual browser window.
The Window object can actually be used instead of thethis object to reference the global object. This is because the Window object not only has the core global properties, but it also has many other globals that have to do with web browsers and client-side Javascript.
Would you like to thankProgrammerInterview.com for being a helpful free resource?Then why not tell a friend about us, orsimply add a link to this page from your webpage using the HTML below.
Link to this page:
Please bookmark with social media, your votes are noticed and appreciated: