+ 2
Functon isArray(myArray) { return myArray.constructor.toString().indexOf("Array") > -1;}
please I don't understand this code...is under constructor func in J.S
1 Answer
+ 17
The constructor property returns the function that created it. Example:
var arr=[1,2];
alert(arr.constructor.toString())// function Array(){[native code]}
This shows that the object is an array.
The code you gave checks this only.
Example 2:
var num=5;
alert (num.constructor.toString())//function Number(){[native code]}
The .toString() method ensures that the function is converted to string and the indexOf() method checks the position in which the given value is. If it is not there, -1 is returned.