Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"{}" 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)
    }
like image 254
Anam Avatar asked Dec 19 '22 06:12

Anam


2 Answers

{} 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.

like image 81
jfriend00 Avatar answered Jan 02 '23 16:01

jfriend00


It means a JavaScript object with no methods or properties.

like image 32
Robert McKee Avatar answered Jan 02 '23 18:01

Robert McKee