为什么不|| 似乎是 JavaScript 中的合并/默认运算符?

发布于 2024-07-08 11:38:47 字数 308 浏览 10 评论 0原文

我见过几个网页说 a = b || 如果 bundefined,则 'blah' 应将 'blah' 分配给 a。 但是,如果我将其输入 Firebug 或在代码中使用它,它会抱怨 FF3/win 上的列表中未定义 b。 有什么提示吗?

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

I've seen a couple of 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 the list on FF3/win. Any hints?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

碍人泪离人颜 2024-07-15 11:38:47

如果 b 存在,并且为 false、null 等,那么它会按照您期望的方式工作。 您需要做的就是在上面的行中输入 var b = null;

如果你仔细想想,这是有道理的。 它基本上做了这样的事情......

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

注意它正在检查 b 的值是否为真......如果 b 不存在,你会得到一个异常。

关于未定义的变量

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

alert(nosuchvariable);
=> throws exception

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

关于检查变量是否存在。

因此,如果我们尝试读取 b 并且没有像 b 这样的变量,我们会得到一个异常。 如果我们试图找出 b 是否已定义,那么这没有帮助。

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

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

因为您知道窗口对象已经存在,所以您可以检查它的字段。
检查 javascript 中不存在的字段将为您提供 undefined 并且不会崩溃,因此您可以进行合并,或者将 undefined 放入变量中或无论什么

对于本地变量(用var声明的东西),你无法检查它们的存在。它们不会“存在”在任何地方全局变量在窗口对象中“存在”的方式,以及任何正常的引用全局变量的尝试都会导致异常:例如:

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

但是有一个异常(据我所知,感谢评论中的 J c),typeof 运算符。 如果您尝试获取不存在的内容的类型,它不会崩溃,它将返回字符串 “未定义” .
这为您提供了一种检查不存在的局部变量的方法。 例如:

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

关于不存在的 DOM 元素

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

事实上,它是一个 DOM 元素并不真正相关。 将 DOM 视为数据库或文件,将元素视为该数据库中的一行或该文件中的单词。 为了用它做任何事情,您必须搜索数据库,找到正确的行,然后提取数据。 数据被放入 JavaScript 对象中。 然后,您可以通过操作该对象来访问它,如果您愿意,还可以将该对象放入变量中。 示例:

document.getElementById('foo');

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

根本不影响合并。

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;

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.

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:

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.

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

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:

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:

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...

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:

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).

It doesn't affect the coalesce at all.

物价感观 2024-07-15 11:38:47

我想你正在寻找这个:

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

I think you're looking for this:

var a = typeof b == 'undefined' ? 'blah' : b;
岁月蹉跎了容颜 2024-07-15 11:38:47

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

,“未定义”是什么意思? 你的意思是Javascript不知道这个变量? 然后您可以使用 window.b 因为“window”是顶级对象,或者首先使用 var b;; 声明 b; 但前提是它一个变量。

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

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

对我有用。

But if I type that into Firebug or use it in code, it complains that b is not defined, at list on 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.

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

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

works for me.

锦上情书 2024-07-15 11:38:47

|| 是 JavaScript 中的短路逻辑 OR 运算符,与 C、C++、Java、C#、Perl、PHP 等中的类似

。 wikipedia.org/wiki/%3F%3F_Operator#JavaScript_implementation" rel="nofollow noreferrer">根据 Wikipedia,如果您在 b 两边加上括号,它将按您的预期工作。

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

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

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

var a = (b) || 'blah';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文