makefile隐含变量_浏览器的隐含全局变量

makefile隐含变量

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.

无论如何,我想测试其他标签和浏览器中其他nameid属性的效果。 除了不创建任何全局变量的Firefox外,其他所有组件都在一定程度上做到了。 相当令人失望。 我测试了IE6、8(加上兼容视图),FF 3.5,Safari 4和Opera 10。

Here's the test page

这是测试页

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

globaldescriptionIEFFSafO
description<meta name="description"... oxxo
robots<meta name="robots"... oxxo
paragraph-id<p id="paragraph-id"... oxoo
paragraph-name<p name="paragraph-name"...xxxo
form-name<form name="form-name"... oxoo
form-id<form id="form-id"... oxoo
input-name<input name="input-name"... xxxx
input-id<input id="input-id"... xxox
link-name<a name="link-name"... oxxo
link-id<a id="link-id"... oxoo
div-name<div name="div-name"... xxxo
div-id<div id="div-id"... oxoo
全球 描述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

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/browser-implied-globals/

makefile隐含变量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值