Javascript using Globals.

Using Javascript Globals.

  • Javascript has a predefined global object, which serves as a placeholder for the global properties and global functions.
  • All the Javascript build-in objects such as Object, Number, String and Date are considered as global properties.
  • Functions and variables that you define outside of a function or object of any kind is to be regarded as a global property.

    function print(str){     // 'print' is a global function
        document.write(str+"<br>");
      }
      var mystr="Hello"; // 'mystr' is a global string
  • The global object has no name, but if your host environment is the web browser, the global object is called window.
  • You can also on the top-level of Javascript use the this keyword as a reference to the global object.
  • You cannot create new global object, as there is no Global constructor function.
  • All pre-defined properties in the global object is non-enumerable in the core of Java Script. All of the other global properties (global variables) allows you to list with a for / in loop like this:

    <script type="text/javascript">
    var variables = ""
    for(var propname in this){
        variables += propname+" is typeof "+typeof this[propname] + "<br>";
    }
    document.write(variables+"<br>");
    </script>
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.