Array is a constructor function to create arrays.
If you type Array in browser console you will get a function definition, something like
function Array() { [native code] }
While if you type Array.prototype in browser console you will get an empty array i.e [ ] i.e. an Array object.
Consider this excerpt
When you type
Then d is an object having two properties which are functions i.e. b and c and you can call
But you cannot call
So just as function b and c are defined in function a. Similarly function map is defined in function Array.
So you cannot call
Therefore they are using
Using
If you type Array in browser console you will get a function definition, something like
function Array() { [native code] }
While if you type Array.prototype in browser console you will get an empty array i.e [ ] i.e. an Array object.
Consider this excerpt
function a(){
console.log('hi');
function b(){console.log('b');}
function c(){console.log('c');}
return {b:b,c:c,d:this}
}
When you type
d = new a();
Then d is an object having two properties which are functions i.e. b and c and you can call
>> d.b() //logs b
>> d.c() //logs c
But you cannot call
a.b() or a.c()
// since function b and c is not property of a.So just as function b and c are defined in function a. Similarly function map is defined in function Array.
So you cannot call
Array.map()
but you have to get an object of Array and call map function on it. Array.prototype
gives us an Array objectTherefore they are using
Array.prototype.map.call(a,func)
http://stackoverflow.com/questions/20153455/why-array-prototype-map-call-instead-of-array-map-call
Using map
to reverse a string
var str = '12345';
Array.prototype.map.call(str, function(x) {
return x;
}).reverse().join('');
// Output: '54321'
// Bonus: use '===' to test if original string was a palindrome
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
No comments:
Post a Comment