Sunday, 23 July 2017

javascript - Type checking for array

When I do: typeof([]) it returns object.



I've heard its not really acceptable to edit the prototypes of JavaScript's inbuilt types.



However, if I do this: Array.prototype.isArray = true; and then the following work:




var arr = [];
var obj = {};

if (arr.isArray)
{
console.log("Array");
}
else
{
console.log("Not array");

}

if (obj.isArray) // undefined
{
console.log("Array");
}
else
{
console.log("Not array");
}



Or is this still not acceptable? What would be a better way around this?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...