简体   繁体   中英

“{}” what does this mean in the below function?

some of these code I understand but "{}" what does this mean...??

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

{} in this context means an empty Javascript object (the same as new Object() ) with no custom properties or methods on it.

So, this statement:

// if el contains a value, do this.el = el, otherwise init this.el to an empty object
this.el = el || {};

is logically equivalent to this:

if (el) {
    this.el = el;   // initialize from function argument
} else {
    this.el = {};   // initialize to an empty object
}

Or in words, "if el contains a non-empty/non-null truthy value, then assign it to this.el otherwise, initialize this.el with an empty object" .

It is a shortcut notation to initalize a variable with the first one in the list that is truthy because Javascript's || operator evaluates until it finds the first truthy operand and then stops the evaluation and takes that operand as the value.

它表示没有方法或属性的JavaScript对象。

// if "this.el" didn't exist, assign it to an empty object {}
this.el = this.el || {};
console.log(el);

Try it here: http://goo.gl/CkRlNB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM