为什么不||似乎在JavaScript中作为coalesce / default运算符工作?

时间:2023-01-01 11:43:45

I've seen a couple web pages say that "a = b || 'blah'" should assign 'blah' to a if b is undefined or null. But if I type that into Firebug or use it in code, it complains that b is not defined, at list on FF3/win. Any hints?

我已经看到一些网页说“a = b ||'blah'”应该将'blah'分配给a,如果b未定义或为null。但是,如果我在Firebug中键入它或在代码中使用它,它会抱怨在FF3 / win列表中没有定义b。任何提示?

Edit: I'm looking for the case where b may not exist at all. For example, a DOM node without an id.

编辑:我正在寻找b可能根本不存在的情况。例如,没有id的DOM节点。

4 个解决方案

#1


3  

I think you're looking for this:

我想你正在寻找这个:

var a = typeof b == 'undefined' ? 'blah' : b;

#2


8  

If b existed, and was false, null, etc, then it works in the way that you would expect. All you'll need to do is on the line above that, put var b = null;

如果b存在,并且是false,null等,那么它将以您期望的方式工作。所有你需要做的就是上面那行,把var b = null;

This makes sense if you think about it. It basically does something like this...

如果你考虑一下,这是有道理的。它基本上做了这样的事......

a = function(){ if(b){return b;} else{ return 'blah' } }();

Note it is checking that the value of b is truthy... if b doesn't exist, you get an exception.

请注意,它检查b的值是否真实...如果b不存在,则会出现异常。

Regarding Undefined variables

"Undefined" in javascript doesn't mean 'variable doesn't exist'. It means "the value of the variable is the special value of undefined". Example:

javascript中的“未定义”并不意味着“变量不存在”。它意味着“变量的值是未定义的特殊值”。例:

alert(nosuchvariable);
=> throws exception

var somevariable; // note it's never assigned
alert(somevariable);
=> This alerts with 'undefined'

Regarding Checking if variables exist.

So if we try to read b and there is no such variable as b, we get an exception. If we're trying to find out if b is defined, then this isn't helpful.

因此,如果我们尝试读取b并且没有b这样的变量,我们会得到一个例外。如果我们试图找出是否定义了b,那么这没有用。

You can see if global variables exist by checking the top-level window object. All global variables are actually just fields in the window object. Example:

您可以通过检查*窗口对象来查看是否存在全局变量。所有全局变量实际上只是窗口对象中的字段。例:

foo = 'Hello';
alert( window.foo );
=> alerts 'Hello'

Because you know the window object already exists, you can check it's fields.
Checking for fields that don't exist in javascript will give you undefined and won't crash, so you can then do the coalesce, or put the undefined in a variable or whatever

因为您知道窗口对象已经存在,所以您可以检查它的字段。检查javascript中不存在的字段会给你未定义并且不会崩溃,因此你可以进行合并,或者将undefined放在变量或者其他内容中

For local variables (things declared with var), you can't check for their existence. they don't "live" anywhere in the way that global variables "live" in the window object, and any normal attempt to reference one will cause an exception: eg:

对于局部变量(用var声明的事物),您无法检查它们的存在。它们不会以全局变量“生活”在窗口对象中的方式“生活”,并且任何引用它的正常尝试都将导致异常:例如:

alert(a);
=> exception because a is meaningless
alert(d45pwiu4309m9rv43);
=> exception because that is equally meaningless

There is however one exception (that I know of, thanks J c in the comments), the typeof operator. If you try and get the type of something that doesn't exist, it won't crash, it will return the string "undefined".
This gives you a way of checking for non-existent local variables. eg:

然而,有一个例外(我知道,感谢评论中的J c),typeof运算符。如果您尝试获取不存在的类型,它将不会崩溃,它将返回字符串“undefined”。这为您提供了一种检查不存在的局部变量的方法。例如:

if( typeof(djfsd) === "undefined" )
  alert('no such variable');

Regarding DOM elements that don't exist

There have been several comments mentioning DOM elements without ID's and so forth...

有几条评论提到没有ID的DOM元素等等......

The fact that it's a DOM element isn't really relevant. Think of the DOM as a database or a file, and an element as a row in that database or word in that file. In order to do anything with it, you have to go searching through the database, find the right row, and pull it's data out. The data gets put into a javascript object. You then access it by manipulating that object, and maybe putting the object in a variable if you like. Example:

事实上,它是一个DOM元素并不真正相关。将DOM视为数据库或文件,将元素视为该数据库中的行或该文件中的单词。为了对它做任何事情,你必须搜索数据库,找到正确的行,然后将数据拉出来。数据被放入javascript对象中。然后,您可以通过操纵该对象来访问它,并且可以根据需要将对象放入变量中。例:

document.getElementById('foo');

this goes into the dom and looks for an element with an ID of 'foo'. If it finds one, it puts some information about that element into a javascript object, and then hands that object back to you. If it can't find the element, it will hand you back null, but all the normal rules still apply (you can stick the null in a variable, or whatever).

这进入dom并查找ID为'foo'的元素。如果找到一个,它会将有关该元素的一些信息放入javascript对象中,然后将该对象交还给您。如果它找不到该元素,它会将您返回null,但所有正常规则仍然适用(您可以将null粘贴在变量中,或者其他)。

It doesn't affect the coalesce at all.

它根本不会影响合并。

#3


0  

|| is the short-circuited logical OR operator in Javascript, much like it is in C, C++, Java, C#, Perl, PHP, etc...

||是Javascript中的短路逻辑OR运算符,非常类似于C,C ++,Java,C#,Perl,PHP等...

According to Wikipedia, if you put parentheses around b, it will work as you expect.

根据*的说法,如果你把括号放在b附近,它会像你期望的那样工作。

var a = (b) || 'blah';

#4


0  

But if I type that into Firebug or use it in code, it complains that b is not defined, at list on FF3/win

但是,如果我将其键入Firebug或在代码中使用它,它会抱怨b未定义,在FF3 / win列表中

What do you mean, "is not defined"? You mean Javascript doesn't know the variable? Then you may use window.b as "window" is the top level object, or first declare b with var b;; but only if it is a variable.

你是什​​么意思,“没有定义”?你的意思是Javascript不知道变量?然后你可以使用window.b,因为“window”是*对象,或者首先用var b声明b ;;但只有它是一个变量。

If it is a DOM element, you may have to try to hunt it down first, for example with document.getElementById:

如果它是DOM元素,您可能必须先尝试搜索它,例如使用document.getElementById:

a = document.getElementById('b') || 'blah'

works for me.

适合我。

#1


3  

I think you're looking for this:

我想你正在寻找这个:

var a = typeof b == 'undefined' ? 'blah' : b;

#2


8  

If b existed, and was false, null, etc, then it works in the way that you would expect. All you'll need to do is on the line above that, put var b = null;

如果b存在,并且是false,null等,那么它将以您期望的方式工作。所有你需要做的就是上面那行,把var b = null;

This makes sense if you think about it. It basically does something like this...

如果你考虑一下,这是有道理的。它基本上做了这样的事......

a = function(){ if(b){return b;} else{ return 'blah' } }();

Note it is checking that the value of b is truthy... if b doesn't exist, you get an exception.

请注意,它检查b的值是否真实...如果b不存在,则会出现异常。

Regarding Undefined variables

"Undefined" in javascript doesn't mean 'variable doesn't exist'. It means "the value of the variable is the special value of undefined". Example:

javascript中的“未定义”并不意味着“变量不存在”。它意味着“变量的值是未定义的特殊值”。例:

alert(nosuchvariable);
=> throws exception

var somevariable; // note it's never assigned
alert(somevariable);
=> This alerts with 'undefined'

Regarding Checking if variables exist.

So if we try to read b and there is no such variable as b, we get an exception. If we're trying to find out if b is defined, then this isn't helpful.

因此,如果我们尝试读取b并且没有b这样的变量,我们会得到一个例外。如果我们试图找出是否定义了b,那么这没有用。

You can see if global variables exist by checking the top-level window object. All global variables are actually just fields in the window object. Example:

您可以通过检查*窗口对象来查看是否存在全局变量。所有全局变量实际上只是窗口对象中的字段。例:

foo = 'Hello';
alert( window.foo );
=> alerts 'Hello'

Because you know the window object already exists, you can check it's fields.
Checking for fields that don't exist in javascript will give you undefined and won't crash, so you can then do the coalesce, or put the undefined in a variable or whatever

因为您知道窗口对象已经存在,所以您可以检查它的字段。检查javascript中不存在的字段会给你未定义并且不会崩溃,因此你可以进行合并,或者将undefined放在变量或者其他内容中

For local variables (things declared with var), you can't check for their existence. they don't "live" anywhere in the way that global variables "live" in the window object, and any normal attempt to reference one will cause an exception: eg:

对于局部变量(用var声明的事物),您无法检查它们的存在。它们不会以全局变量“生活”在窗口对象中的方式“生活”,并且任何引用它的正常尝试都将导致异常:例如:

alert(a);
=> exception because a is meaningless
alert(d45pwiu4309m9rv43);
=> exception because that is equally meaningless

There is however one exception (that I know of, thanks J c in the comments), the typeof operator. If you try and get the type of something that doesn't exist, it won't crash, it will return the string "undefined".
This gives you a way of checking for non-existent local variables. eg:

然而,有一个例外(我知道,感谢评论中的J c),typeof运算符。如果您尝试获取不存在的类型,它将不会崩溃,它将返回字符串“undefined”。这为您提供了一种检查不存在的局部变量的方法。例如:

if( typeof(djfsd) === "undefined" )
  alert('no such variable');

Regarding DOM elements that don't exist

There have been several comments mentioning DOM elements without ID's and so forth...

有几条评论提到没有ID的DOM元素等等......

The fact that it's a DOM element isn't really relevant. Think of the DOM as a database or a file, and an element as a row in that database or word in that file. In order to do anything with it, you have to go searching through the database, find the right row, and pull it's data out. The data gets put into a javascript object. You then access it by manipulating that object, and maybe putting the object in a variable if you like. Example:

事实上,它是一个DOM元素并不真正相关。将DOM视为数据库或文件,将元素视为该数据库中的行或该文件中的单词。为了对它做任何事情,你必须搜索数据库,找到正确的行,然后将数据拉出来。数据被放入javascript对象中。然后,您可以通过操纵该对象来访问它,并且可以根据需要将对象放入变量中。例:

document.getElementById('foo');

this goes into the dom and looks for an element with an ID of 'foo'. If it finds one, it puts some information about that element into a javascript object, and then hands that object back to you. If it can't find the element, it will hand you back null, but all the normal rules still apply (you can stick the null in a variable, or whatever).

这进入dom并查找ID为'foo'的元素。如果找到一个,它会将有关该元素的一些信息放入javascript对象中,然后将该对象交还给您。如果它找不到该元素,它会将您返回null,但所有正常规则仍然适用(您可以将null粘贴在变量中,或者其他)。

It doesn't affect the coalesce at all.

它根本不会影响合并。

#3


0  

|| is the short-circuited logical OR operator in Javascript, much like it is in C, C++, Java, C#, Perl, PHP, etc...

||是Javascript中的短路逻辑OR运算符,非常类似于C,C ++,Java,C#,Perl,PHP等...

According to Wikipedia, if you put parentheses around b, it will work as you expect.

根据*的说法,如果你把括号放在b附近,它会像你期望的那样工作。

var a = (b) || 'blah';

#4


0  

But if I type that into Firebug or use it in code, it complains that b is not defined, at list on FF3/win

但是,如果我将其键入Firebug或在代码中使用它,它会抱怨b未定义,在FF3 / win列表中

What do you mean, "is not defined"? You mean Javascript doesn't know the variable? Then you may use window.b as "window" is the top level object, or first declare b with var b;; but only if it is a variable.

你是什​​么意思,“没有定义”?你的意思是Javascript不知道变量?然后你可以使用window.b,因为“window”是*对象,或者首先用var b声明b ;;但只有它是一个变量。

If it is a DOM element, you may have to try to hunt it down first, for example with document.getElementById:

如果它是DOM元素,您可能必须先尝试搜索它,例如使用document.getElementById:

a = document.getElementById('b') || 'blah'

works for me.

适合我。