我正在使用Easel JS开展项目。打开了Easel文件,第一行代码让我困惑:
this.createjs = this.createjs||{};
我知道在设置画布时调用createjs,或者创建一个位图来添加到画布。但我不理解这一行的语法 - 将this.createjs或(我猜的是)一个空白对象分配给this.createjs?
答案 0 :(得分:6)
this.createjs = this.createjs||{};
如果this.createjs
不可用/任何falsy
值,那么您将{}
空对象分配给this.createjs
。
更像是,
var a,
b;
b = a || 5;
由于a
目前没有任何价值,5
将被分配到b
。
答案 1 :(得分:2)
正确。这可确保如果this.createjs
尚不存在,则为其分配一个空对象。 ||
是一个或操作符 - 如果左侧的this.createjs
评估为falsy,则会指定右侧。
答案 2 :(得分:2)
this.createjs = this.createjs||{};
如果this.createjs是假的,this.createjs将是一个新的空对象
您可以通过
替换它if (!this.createjs){
this.createjs = {};
}
答案 3 :(得分:1)
||
表示or
。
在该上下文中,this.createjs
表示等于存在/非空/定义this.createjs
其他方式{}