gpt4 book ai didi

javascript - "{}"在下面的函数中是什么意思?

转载 作者:搜寻专家 更新时间:2023-11-01 04:56:52 25 4
gpt4 key购买 nike

其中一些代码我理解,但是“{}”是什么意思...?

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 包含非空/非空真值,则将其分配给 this.el 否则, 用空对象初始化 this.el

这是一种用列表中第一个为真值的变量初始化变量的快捷方式,因为 Javascript 的 || 运算符会一直求值,直到找到第一个真值操作数,然后停止求值并接受它操作数作为值。

关于javascript - "{}"在下面的函数中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com