In response to a previous question, I received this helpful answer:
for (var i in someArray) {
if ({}.hasOwnProperty.call(someArray, i))
alert(someArray[i]);
}
My questions are:
-
Where can I read about the {} construct? I cannot find it in the jQuery docs, and it is impossible to google for.
-
Where can I read about the call() function. Searching the jQuery API site does not turn up anything seemingly related.
Thanks.
{}is one way to declare an empty object. It is called object literal syntax and you can read more about it here.The
call()method is a JavaScript method (not jQuery). Again, you can read more about it here. Basically,call()allows you to change the value ofthisinside the function you’re callingcall()on. It is related toapply();Looking at the code in particular, we’re looping over an array and using the
hasOwnPropertymethod to check the value (i) exists on thesomeArrayarray (as opposed to being in the prototype chain ofsomeArray.As for why we’re using
{}.hasOwnPropertyas opposed tosomeArray.hasOwnProperty, I guess that the user might be protecting againsthasOwnPropertybeing declared onsomeArray(by using an empty object). If he hadn’t done this, then the following could have been possible;Or even;