What does this JS Syntax do?
Why would I put brackets around the 开发者_开发知识库true
here.... doesn't that make an array? Does it do something else?
$e.trigger("onlyShowIfChange", [true]);
Yes, that makes an Array with one element — true
.
It doesn't do anything else.
Sometimes, method calls may require an array, and if you don't have an Array already, you can conveniently wrap your parameter(s) with []
.
A common case is the Function.apply method. The second parameter is argArray:
var func = function(a, b) {
return a + b;
};
func.apply(null, [1, 3]); // returns 4
.trigger( eventType, extraParameters )
eventType: A string containing a JavaScript event type, such as click or submit.
extraParameters: An array of additional parameters to pass along to the event handler.
The second parameter has to be an array
$e.trigger("onlyShowIfChange", [true, false]);
$e.bind("onlyShowIfChange", function(ev, trueBool, falseBool) {
...
});
This means if you want to pass extra parameters to your event binding functions you have to pass in an array of parameters to trigger as the second argument.
It makes an array with true
as its only value, but probably you have found this syntax in some documentation and in that case means that true
is optional.
精彩评论