Reading a book by David Flenegan stumbled upon such a code.
var isArray = Function.isArray || function(o) { return typeof o === "object" && Object.prototype.toString.call(o) === "[object Array]"; }; This code by definition defines a function for checking the type of a variable on an array. But I do not understand why this thing is needed: Function.isArray || . I need an explanation, on the Internet, the search for this was not successful.
PS The title for the title is very bad and I understand, but I can’t do anything about it.
Update:
- Several people here answered supposedly see how Boolean logic works. But I know how it works (lazy calculation from left to right)
- Special thanks to
@andreymalhe imbued with what I was asking.
Update 2:
In general, the following chapter has the same code:
Array.join = Array.join || function(a,sep) { return Array.prototype.join.call(a,sep); }; Array.slice = Array.slice || function(a,from,to) { return Array.prototype.slice.call(a,from,to); }; Array.map = Array.map || function(a, f, thisArg) { return Array.prototype.map.call(a, f, thisArg); } Is this code needed to define your own functions? And if they are defined not to redefine? Then why is it not written like this Array.prototype.join || , Array.slice = Array.prototype.slice || , Array.map = Array.prototype.map || ?
Array.isArray- Grundy