Not defining the type of variable (Automatic global variables)

This question already has an answer here:

  • Define global variable in a JavaScript function 11 answers

  • In JavaScript, creating a variable without var is the same as setting it on the global object as a property (and in the browser, global is window :

    nameWithoutVar = 1;
    // the above is the same as
    window.nameWithoutVar = 1;
    

    This means that any other script loaded in the browser for the page can access nameWithoutVar , the same as they can access location , document , etc.

    Global variables are considered an extremely bad idea for this reason, because everything is using the same namespace. If you must use a global variable†, be sure to document it, and try to namespace it so that it is unlikely to conflict with any other variable.

    †If you're not sure, you probably don't have to.

    链接地址: http://www.djcxy.com/p/95022.html

    上一篇: node.js全局变量?

    下一篇: 不定义变量的类型(自动全局变量)