我来自C#背景,需要更熟悉JS.我正在读一本书,在这个例子中:
var as = document.getElementsByTagName('a');
for(var i=0;i<as.length;i++){
t=as[i].className;
//Check if a link has a class and if the class is the right one
if(t && t.toString().indexOf(popupClass) != -1)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
该部分if声明没有任何意义.什么是if(t)?我习惯if语句检查布尔值,但是t是一个字符串,对吗?
if语句确实输入了强制.
if (o) block;
只会运行块.如果o是"真相"
以下值是假的:
"", null, undefined, false, 0, NaN
其余的都是真的.
这里可以找到一篇好文章
更重要的是,你有很多死代码.
var as = document.getElementsByTagName('a');
for(var i=0;i<as.length;i++){
t=as[i].className;
// no need to check for whether t exists. No need to cast it to a string
if(t.indexOf(popupClass) !== -1) // use !== for strict equality
{
//...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |