Why are square brackets used in javascript function calls? JQuery

C: Antwerp 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]); } 

why [contents] and not contents ?

+4
source share
3 answers

The requirement for the jQuery trigger function is that the second parameter is an array (up to 1.6.2), so wrapping makes it an array. From trigger documents (underline me):

 $('#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 for the handler event, but if additional parameters are specified during .trigger (), these parameters will be passed along with the handler as well. To pass multiple parameters, use an array as shown here. Starting with jQuery 1.6.2, one parameter can be passed without using an array.

So, starting with 1.6.2, there is virtually no need to wrap a single argument in an array.

+5
source

If the function expects an array, then you put one or more elements in square brackets. For example, trigger function declaration

.trigger (eventType [, extraParameters])

Since you can specify more than one additional parameter, it takes an array of them. If you have only one additional parameter, for example, โ€œcontentโ€ in your case, you can put it in an array (or if you have only one parameter, you also can not put it in an array, since jQuery now accepts anyway) .

+1
source

Since Trin pointed to an array of [content] with one element.

I donโ€™t know why, but if you change it to content instead of [content], it will search for the query in the whole word.

If his [content] will only search at the beginning.

0
source

All Articles