阅读leaflet.js的代码,我遇到了一个我不太了解的方法:
var events = this._leaflet_events = this._leaflet_events || {};
Run Code Online (Sandbox Code Playgroud)
可以简化为
var a = b = b || {};
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这个指令是一个多重的左手赋值,它是右关联的,这意味着首先,JavaScript将运行
b = b || {} //if b exists, returns b, else return an empty object
Run Code Online (Sandbox Code Playgroud)
, 然后
a = b // returns the output of the preceding instruction
Run Code Online (Sandbox Code Playgroud)
这对我没有意义.为什么不写呢:
a = b || {};
Run Code Online (Sandbox Code Playgroud)
完整背景:
addEventListener: function( /*string*/ type, /*function */ fn, /*(optional) object*/ context){
var events = this._leaflet_events = this._leaflet_events || {};
events[type] = events[type] || {};
events[type].push({
action: fn, …Run Code Online (Sandbox Code Playgroud) 你能帮我理解这个javascript RegExp:
cbreg = new RegExp('((^|\\?|&)' + cbkey + ')=([^&]+)')
// where cbkey is a string
Run Code Online (Sandbox Code Playgroud)
我对这(^|\\?|&)部分感到困惑.这意味着什么?
谢谢 !