Cross Browser Valid JavaScript Names

后端 未结 2 1532
长发绾君心
长发绾君心 2020-12-18 15:49

Before you quickly post with \"RTFM\" or with a bunch of links I have visited, I am fully aware of the documented reserved variable names not to use.

The best lists

相关标签:
2条回答
  • 2020-12-18 16:21

    What I am asking for are the variable names that are invalid on 1 or 2 browsers only.

    If any such words exist, that browser would be non–compliant with ECMA-262. Only the reserved words in ECMA-262 are "invalid variable names" (provided the name is otherwise compliant with the criteria for variable names, such as allowed characters).

    Why isn't [print] documented as a reserve keyword?

    Reserved words can only be defined by standards that have a concept of reserved word, the only standard relevant to scripting DOMs in browsers that has that concept is ECMA-262. And it doesn't list print as a reserved word, so it isn't one.

    There are various DOM standards that define host objects and their properties, the closest they might get to the concept of reserved word is that of a read–only property, or one that is not writeable. window.print is defined in HTML5, which doesn't define it as not being writable or that it should throw errors if assigned to or attempts are made to modify it. So it doesn't exhibit any behaviour approaching that of a reserved word.

    Is there a big list of variable names to avoid?

    No, because there aren't any. However, host objects (like window) have default properties that are writeable, you should avoid overwriting them. They should be listed in various places, like the HTML5 specification and browser vendor documentation.

    e.g. the following links for the window object:

    1. W3C HTML5 window object: http://www.w3.org/TR/html5/browsers.html#the-window-object
    2. MDN window object: https://developer.mozilla.org/en/DOM/window
    3. MSDN window object: http://msdn.microsoft.com/en-us/library/ms535873(v=vs.85).aspx

    In addition, there is a simple for..in loop (per Berji's answer) to discover the enumerable properties at a particular time, however that may not be a comprehensive list of all possible property names and will include user defined properties along with default browser properties without distinction.

    It is a better strategy to adopt a naming convention that avoids likely property names and minimise the use of global variables (i.e. user defined properties of the window object).

    0 讨论(0)
  • 2020-12-18 16:31

    If you want a big list of browser-defined variables to extend the official list of reserved keywords, type the following into your console on an empty page (about:blank):

    >>> Object.getOwnPropertyNames(window)
    >>> Object.getOwnPropertyNames(Object.getPrototypeOf(window)) // sometimes not Object
    

    which will yield an Array of strings you definitely should not use. It includes "print", too.

    Yet it will be browser-dependent, because some do not implement all the cool HTML5-drafted stuff that uses the Window interface; e.g. you won't find "Worker" in IE9, "openDatabase" in FF and so on. Also, it might not list legacy properties like "onload", altough you will be able to to get a property descriptor for it (and "onload" in window === true).

    0 讨论(0)
提交回复
热议问题