Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

variables - Cross Browser Valid JavaScript Names

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 I could find are here:

http://es5.github.com/x7.html#x7.6.1.1 and http://www.javascripter.net/faq/reserved.htm

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

For example I can define print but this will cause an error as print is already defined on the global scope window. Why isn't this documented as a reserve keyword?

Is there a big list of variable names to avoid?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...