在 if 块中定义 JavaScript 变量有什么问题? [英] What's wrong with defining JavaScript variables within if blocks?

查看:20
本文介绍了在 if 块中定义 JavaScript 变量有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的代码:

if (condition) {
var variable = blah;
}

if (differentcondition) {
var variable = blah;
}

这是正确的吗?

我假设如果条件不返回 true,则不会分配变量.

I assume that the variable wouldn't be assigned if the condition doesn't return true.

JSLint 一直告诉我,变量已经定义.

JSLint keeps on telling me, variable already defined.

我做错了吗?

谢谢.

好的,这是我的实际用例,我正在做这样的事件委托:

OK, Here's my actual usecase, i'm doing event delegation like this:

$("#container").click(function (event){ 

    if ($(event.target).is('img.class1')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

    if ($(event.target).is('img.class2')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

    // This condition is mutually exclusive to the above 2
    if ($(event.target).is('img.class3')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

    // This condition is mutually exclusive to 1 and 2 but not to 3
    if ($(event.target).is('img.class4')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

});

实际上这两个类并不相互排斥.

Actually these 2 classes are not mutually exclusive.

这对我有用,但正确吗?

This works for me, but is it correct?

答案非常有用,但我仍然不明白我应该如何设置这里的变量.

The answers were very informative, but I still don't understand how I should set up the variables here.

其实我也想说,有的条件是互斥的,有的条件不是.

Actually I also want to say that certain conditions are mutually exclusive, and certain conditions are not.

我应该如何构建它?

我可能应该从一开始就使用这个例子.

I probably should've used this example from the beginning.

推荐答案

因为 javascript 有一种叫做提升"的东西,它可以让你的代码做一些看起来不应该做的事情.基本上,这意味着 javascript 解释器会将所有 var 声明(无论它们在函数体中的哪个位置)移动到函数体的顶部.然后它将所有函数定义移动到顶部,就在所有变量的下方.然后它将完成函数的编译.

Because javascript has something called "Hoisting" which makes your code do things it doesn't look like it should be doing. Basically, that means a javascript interpreter will move all var declarations, regardless of where they are in the body of a function, to the top of the function body. Then it will move all function definitions to the top, just under all the vars. Then it will finish compiling the function.

将 var 放入 if 语句中并不违反语言的规则",但这意味着,由于 var 提升,无论 if 语句的条件是否满足,都会定义该 var.

Putting a var inside an if statement is not against "the rules" of the language, but it means that, because of var hoisting, that var will be defined regardless of whether the if statement's condition is satisfied.

还要记住,提升不包括赋值,所以正如其他人指出的那样,var 声明将移到顶部,并且在稍后分配之前保持未定义状态.

Keep in mind also that hoisting does not include the assignment, so as others have pointed out, the var declarations will be moved to the top, and left undefined until they're assigned later.

这是一个在当时看来是个好主意的功能示例,但结果证明它更令人困惑而不是有用.

This is an example of a feature that must have seemed like a good idea at the time, but it has turned out to be more confusing than helpful.

这篇关于在 if 块中定义 JavaScript 变量有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆