跨浏览器有效的JavaScript名称 [英] Cross Browser Valid JavaScript Names
问题描述
在您快速发布RTFM或我访问过的一堆链接之前,我完全了解所记录的保留变量名称不能使用。
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
和
http://www.javascripter.net/faq/reserved.htm
我是什么我要求的是仅在1或2个浏览器上无效的变量名。
What I am asking for are the variable names that are invalid on 1 or 2 browsers only.
例如我可以定义 print
但这会导致错误,因为已在全局范围窗口
上定义了打印。为什么不记录为保留关键字?
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?
推荐答案
如果你想要一个大的浏览器定义变量列表来扩展保留关键字的官方列表,在空白页面上输入以下内容(关于:空白):
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
将产生一个数组你肯定不应该使用的字符串。它还包括print
。
which will yield an Array of strings you definitely should not use. It includes "print"
, too.
但它依赖于浏览器,因为有些并不是全部实现使用 Window
界面的HTML5草稿很酷的东西;例如你不会在IE9中找到Worker
,在FF中找不到openDatabase
等等。此外,它可能不会列出遗留属性,如onload
,尽管您将能够获取它的属性描述符(以及onload在窗口中=== true
)。
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
).
这篇关于跨浏览器有效的JavaScript名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!