JavaScript 点符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2001360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript dot notation
提问by skarama
The following line is apparently written best in dot notation. I am trying to clean my JavaScript code to make it strict. What does it mean?
下面这行显然用点符号写得最好。我正在尝试清理我的 JavaScript 代码以使其严格。这是什么意思?
if (ie||ns6)
{
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";
}
I added some context to my line of code, in case this helps? I know nothing about DOM. I am not trying to support Internet Explorer 4, this is not my code and I wouldn't be able to write JavaScript myself. I am only trying to get it compliant and the JSLinttool says about this line:
我在我的代码行中添加了一些上下文,以防万一这有帮助?我对DOM一无所知。我不想支持 Internet Explorer 4,这不是我的代码,我自己也无法编写 JavaScript。我只是想让它合规,JSLint工具说明了这一行:
Problem at line 17 character 43: ['dhtmltooltip'] is better written in dot notation.
第 17 行第 43 行的问题:['dhtmltooltip'] 最好用点表示法编写。
回答by Quentin
There are two ways to access properties of an object in JavaScript.
Dot notation
点符号
foo.bar.baz
Square bracket notation
方括号表示法
foo['bar']['baz']
You are using the latter in part of your code.
您在代码的一部分中使用了后者。
Douglas Crockford, who wrote JSLint (a tool which gives that error message), is of the opinion that is is better to use dot notation where possible.
编写 JSLint(提供该错误消息的工具)的 Douglas Crockford 认为,在可能的情况下最好使用点表示法。
回答by CMS
JSLint wants this:
JSLint 想要这个:
var tipobj= document.all ? document.all.dhtmltooltip
: document.getElementById
? document.getElementById("dhtmltooltip")
: "";
But nowadays is completely safe to assume that document.getElementByIdexists, it was introduced on the DOM Level Core 2as of year 2000.
但是现在可以完全安全地假设document.getElementById存在,它是在2000年的DOM Level Core 2上引入的。
document.allis dead, unless you try to support really old browsers like IE4 (12 year old!):
document.all已经死了,除非你尝试支持像 IE4 这样的老浏览器(12 岁!):
var tipobj = document.getElementById("dhtmltooltip");
The two above snippets are a good example about the complexity costof supporting very old browserversions:
以上两个片段是关于支持非常旧的浏览器版本的复杂性成本的一个很好的例子:
回答by Li0liQ
The following seems to be more user-friendly.
以下似乎对用户更友好。
var tipobj;
if (document.all)
tipobj = document.all["dhtmltooltip"];
else if (document.getElementById)
tipobj = document.getElementById("dhtmltooltip");
else
tipobj = "";
回答by Hank Gay
It's using capability checking to retrieve an element with the id dhtmltooltipand falling back to an empty Stringif there is not a capability for doing the retrieval.
它使用能力检查来检索具有 id 的元素,如果没有进行检索的能力,则dhtmltooltip返回为空String。
UPDATE: As others have pointed out, the check for getElementByIdshould be first, and could probably be omitted since any browser that could be called "modern" with a straight face has had it for a long time.
更新:正如其他人所指出的,getElementById应该首先检查,并且可能会被省略,因为任何可以被称为“现代”的浏览器都已经使用了很长时间。
UPDATE 2: With the new context, JSLint is complaining that it isn't document.all.dhtmltooltip. You should probably just rewrite the whole thing as:
更新 2:在新的上下文中,JSLint 抱怨它不是document.all.dhtmltooltip. 您可能应该将整个内容重写为:
var tipobj = document.getElementById("dhtmltooltip");
and be done with it.
并完成它。
回答by avpx
A quick Google search says that document.allis only used to support IE4. It is an array that allows the browser to access different parts of the DOM (see here.)
一个快速的谷歌搜索说document.all它只用于支持 IE4。它是一个数组,允许浏览器访问 DOM 的不同部分(请参阅此处。)
The code you posted first checks if document.all exists. If not, it sets tipobjto "". Now, beyond this, it's not really worth deciphering the line you posted unless you reallywant IE4 support. Since very few people still use IE4 and this piece of code isn't compliant to any modern standards, I'd just drop that line and set tipobjto "".
您首先发布的代码检查 document.all 是否存在。如果不是,则设置tipobj为""。现在,除此之外,除非您真的想要 IE4 支持,否则真的不值得破译您发布的行。由于很少有人仍然使用 IE4 并且这段代码不符合任何现代标准,因此我将删除该行并将其设置tipobj为"".
回答by chills42
It looks like the only real issues are formatting/syntax. This should work exactly the same and conform to javascript best practice. The main difference is using javascript dot notation instead of bracket notation.
看起来唯一真正的问题是格式/语法。这应该完全相同并符合javascript最佳实践。主要区别是使用 javascript 点表示法而不是括号表示法。
if (ie || ns6) {
var tipobj = document.all ? document.all.dhtmltooltip : document.getElementById ? document.getElementById("dhtmltooltip") : "";
}
回答by Michael Sloat
why not just use:
为什么不使用:
var tipobj = dhtmltooltip.id
Not sure why the long version is required unless the dot notation doesnt work in all browsers?
不确定为什么需要长版本,除非点符号在所有浏览器中都不起作用?
回答by Tyson Cadenhead
If the dot notation is a problem, you can always set the /*jslint sub: true */ option to override it.
如果点符号有问题,您始终可以设置 /*jslint sub: true */ 选项来覆盖它。
回答by Salvador Dali
As is was answered by Quentin both ways are valid.
正如 Quentin 所回答的那样,两种方式都是有效的。
One of the reasons why I prefer to use elem.barinstead of elem['bar']is that it saves 3 characters. Surely this is not a big improvement, but a free 3 bites per assignment is not bad.
我更喜欢使用elem.bar而不是使用elem['bar']它的原因之一是它可以节省 3 个字符。这当然不是一个很大的改进,但每次任务免费 3 口也不错。


