makefile隐含变量
Like it's not bad enough that JavaScript has implied globals (forget var
and you create a global), but the browsers have decided it's a good idea to add more pollution to the global namespace.
好像JavaScript隐含了全局变量还不够糟(忘记var
并创建一个全局变量),但是浏览器认为向全局命名空间添加更多的污染是个好主意。
This has been a source of frustration before with IE, it's really hard to understand the logic behind it, but it's also happening in other browsers.
在使用IE之前,这一直是令人沮丧的根源,很难理解它背后的逻辑,但是在其他浏览器中也是如此。
Consider this:
考虑一下:
<meta name="description" content="test me" />
A normal META tag, right? But in IE this will create in a global variable called "description" pointing to that DOM node. Yep.
普通的META标签,对不对? 但是在IE中,这将在称为该DOM节点的全局变量“ description”中创建。 是的
alert(description.content); // "test me"
That's pretty annoying. Even more annoying is that getElementById('description')
will also point to the DOM node, although it doesn't even have an ID.
真烦人。 更令人讨厌的是,尽管getElementById('description')
甚至没有ID,但它也指向DOM节点。
测试诞生 (A test is born)
Anyway, I wanted to test the effect of other name
and id
attributes in different tags and different browsers. With the exception of Firefox which doesn't create any globals, all other did to some degree. Rather disappointing. I tested IE6, 8 (plus compat view), FF 3.5, Safari 4 and Opera 10.
无论如何,我想测试其他标签和浏览器中其他name
和id
属性的效果。 除了不创建任何全局变量的Firefox外,其他所有组件都在一定程度上做到了。 相当令人失望。 我测试了IE6、8(加上兼容视图),FF 3.5,Safari 4和Opera 10。
And below are the results. The yellow x
means that testing for the presence of this global returned "undefined", the white o
means that the global variable points to an object. So for example continuing with the meta example above, typeof window.description
will return undefined in FF (yellow x
) and object in IE (white o
).
以下是结果。 黄色的x
表示测试此全局变量是否返回“未定义”,白色的o
表示全局变量指向一个对象。 因此,例如继续上面的meta示例, typeof window.description
将在FF中返回未定义(黄色x
),在IE中返回对象(白色o
)。
global | description | IE | FF | Saf | O |
---|---|---|---|---|---|
description | <meta name="description"... | o | x | x | o |
robots | <meta name="robots"... | o | x | x | o |
paragraph-id | <p id="paragraph-id"... | o | x | o | o |
paragraph-name | <p name="paragraph-name"... | x | x | x | o |
form-name | <form name="form-name"... | o | x | o | o |
form-id | <form id="form-id"... | o | x | o | o |
input-name | <input name="input-name"... | x | x | x | x |
input-id | <input id="input-id"... | x | x | o | x |
link-name | <a name="link-name"... | o | x | x | o |
link-id | <a id="link-id"... | o | x | o | o |
div-name | <div name="div-name"... | x | x | x | o |
div-id | <div id="div-id"... | o | x | o | o |
全球 | 描述 | IE浏览器 | FF | 萨夫 | Ø |
---|---|---|---|---|---|
描述 | <meta name =“ description” ... | Ø | X | X | Ø |
机器人 | <meta name =“ robots” ... | Ø | X | X | Ø |
段落编号 | <p id =“ paragraph-id” ... | Ø | X | Ø | Ø |
段落名称 | <p name =“ paragraph-name” ... | X | X | X | Ø |
表格名称 | <form name =“ form-name” ... | Ø | X | Ø | Ø |
形式编号 | <form id =“ form-id” ... | Ø | X | Ø | Ø |
输入名称 | <input name =“输入名称” ... | X | X | X | X |
输入ID | <input id =“ input-id” ... | X | X | Ø | X |
链接名称 | <a name =“链接名称” ... | Ø | X | X | Ø |
链接ID | <a id =“ link-id” ... | Ø | X | Ø | Ø |
div名称 | <div name =“ div-name” ... | X | X | X | Ø |
div-id | <div id =“ div-id” ... | Ø | X | Ø | Ø |
所以...? (So...?)
So this is a useless feature if you ask me. Not reliable, not cross-browser, maybe considered convenient back when rollover buttons and animated gifs were all the rage (and animated window.status, remember?), but today can only cause troubles where you least expect it. Should be removed in future browser versions.
因此,如果您问我,这是一个无用的功能。 不可靠,也不是跨浏览器,当滚动按钮和动画gif泛滥时(可能还带有动画window.status,还记得吗?),这可能被认为很方便,但是今天只能在您最不希望的地方引起麻烦。 应该在将来的浏览器版本中删除。
For the time being we just have to remember to always declare and initialize our local variables because it looks like someone else might also decide to do so for us. Which can lead to errors if we assume too much.
目前,我们只需要记住始终声明和初始化我们的局部变量,因为看起来其他人可能也决定为我们这样做。 如果我们假设太多,可能会导致错误。
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
makefile隐含变量