Reputation: 37078
So I'm used to automatically writing things such as this:
Array.prototype.slice.call(arguments);
--and similar such applications of various methods via Array.prototype.XXX.call
or Object.prototype.XXX.call([])
.
So I went to try out the Array.isArray
method which I didn't know existed, and automatically wrote Array.prototype.isArray.call
, which of course failed since isArray
isn't defined on the prototype.
But then I tried Array.isArray.call([])
slightly unthinkingly and got false
, which confused me.
It worked fine on the third try when I went the plain ol' boring way (the correct usage) and simply wrote Array.isArray(thing_to_be_tested)
, but why did I get false
on the previous attempt? I don't understand why that doesn't work.
Upvotes: 1
Views: 123
Reputation: 236062
The first thing .call()
expects is the context. So you're basically calling .isArray()
with the context of an Array, but an undefined
value.
Call instead:
Array.isArray.call( null, [] );
or to make it more realistic:
var isarr = Function.prototype.call.bind( Array.isArray, null );
Now you can call that like
isarr( [] );
Upvotes: 2
Reputation: 94121
Because Array.isArray
has one argument, which you are not passing along. You are simply passing the this
value, which is not used in the function:
Array.isArray.call(null, [])
Upvotes: 4