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

查看:107
本文介绍了在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有一个名为Hoisting的东西,它使你的代码做的事情看起来不像它应该做的那样。基本上,这意味着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提升,将定义var而不管是否满足if语句的条件。

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天全站免登陆