javascript:这是一个有条件的任务吗? [英] javascript: is this a conditional assignment?

查看:106
本文介绍了javascript:这是一个有条件的任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Google Analytics跟踪代码:

From the google analytics tracking code:

var _gaq = _gaq || [];

这是如何工作的?

是它是一个条件变量值赋值?
与说法相同:

Is it a conditional variable value assignment? Is it the same as saying:

if( !(_gaq) ) {_gaq = []; }

推荐答案

如果它是一个真值,则运算符( || )将返回左侧,否则它将返回右侧。

The or operator (||) will return the left hand side if it is a true value, otherwise it will return the right hand side.

它与你的第二个例子非常相似,但由于它使用 var 关键字,它还建立了一个本地变量的范围。

It is very similar to your second example, but since it makes use of the var keyword, it also establishes a local scope for the variable.

这篇关于javascript:这是一个有条件的任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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