gpt4 book ai didi

javascript - 为什么在 javascript 函数调用中使用方括号?查询

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:03 24 4
gpt4 key购买 nike

来自: andrew whittakers example showing result numbers in a custom jquery autocomplete implementation

 _response: function(contents){
$.ui.autocomplete.prototype._response.apply(this, arguments);
$(this.element).trigger("autocompletesearchcomplete", [contents]);
}

为什么是 [contents] 而不是 contents

最佳答案

jQuerytrigger 函数要求第二个参数是一个数组(1.6.2 之前的版本),因此需要包装使其成为一个数组。来自trigger docs (由我强调):

$('#foo').bind('custom', function(event, param1, param2) {
alert(param1 + "\n" + param2);
});
$('#foo').trigger('custom', ['Custom', 'Event']);

The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a .trigger() call, these parameters will be passed along to the handler as well. To pass more than one parameter, use an array as shown here. As of jQuery 1.6.2, a single parameter can be passed without using an array.

因此从 1.6.2 开始,实际上没有必要将单个参数包装在数组中。

关于javascript - 为什么在 javascript 函数调用中使用方括号?查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967025/

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