[英]“{}” what does this mean in the below function?
其中一些代碼我理解,但“{}”這意味着什么......?
var Accordion = function(el, multiple) {
this.el = el || {};
this.multiple = multiple || false;
// Variables
var link = this.el.find('.link');
// Eventos
link.on('click', {el: this.el, multiple: this.multiple},this.dropdown)
}
此上下文中的{}
表示一個空的Javascript對象(與new Object()
相同),上面沒有自定義屬性或方法。
所以,這句話:
// if el contains a value, do this.el = el, otherwise init this.el to an empty object
this.el = el || {};
在邏輯上等同於:
if (el) {
this.el = el; // initialize from function argument
} else {
this.el = {}; // initialize to an empty object
}
或者用文字說, “如果el
包含非空/非空的truthy值,則將其分配給this.el
否則,用空對象初始化this.el
” 。
這是一個快捷符號,用於使變量初始化,列表中的第一個變量是真實的,因為Javascript的||
運算符求值直到找到第一個真值操作數,然后停止計算並將該操作數作為值。
它表示沒有方法或屬性的JavaScript對象。
// if "this.el" didn't exist, assign it to an empty object {}
this.el = this.el || {};
console.log(el);
在這里試試: http : //goo.gl/CkRlNB
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.