In JavaScript, I wonder why I can do this:
[].slice.apply([1,2,3], [0, 2]) // returns [1, 2]
Which is as far as I understand equivalent of this:
Array.prototype.slice.apply([1,2,3], [0, 2]); // returns [1, 2]
But I can’t do this:
{}.hasOwnProperty.apply(a, ['a']);
Which would allow to not type this long line:
Object.prototype.hasOwnProperty.apply(a, ['a']); // returns true or false
Well, while I am at it, I also see that I can actually do all of these:
''.indexOf.apply('asdasd', ['s']); // returns 1
true.toString.apply(true); // returns // "true" as string
What the deal with {} ? 🙂 Why is it special?
Is
hasOwnProperty('a')a typo?But that doesn’t quite work due to JavaScript interpreting
{}as block delimiters. Just add parentheses:Check it: