Reputation: 452
W3schools array article reads
The
instanceof
operator returns true if an object is created by a given constructor
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits instanceof Array // returns true
But I am not sure which is the constructor
in the above code. Is it Array()
? Does that mean that the array literal []
is a shortcut for new Array()
?
Upvotes: 0
Views: 401
Reputation: 6922
Actually, that definition is a bit confusing.
The instanceof operator does not actually check whether an object was created by a constructor function. It checks whether it inherits from the constructor prototype (the inheritance need not be direct).
The difference is subtle, but important. Let me elaborate:
Imagine that we have two constructors, and both have the same prototype:
var F = function() {};
var G = function() {};
var FakeConstructor = function() { this.name = "FakeConstructor"; };
var obj = {};
F.prototype = obj;
G.prototype = obj;
// F prototype and G prototype are the very same object. We set the constructor to FakeConstructor just to point out that it is irrelevant for the instanceof operator
G.prototype.constructor = FakeConstructor;
var g = new G();
var f = new F();
f instanceof F;// true
f instanceof G;// true
g instanceof F;// true
g instanceof G;// true
Even though f
and g
have been constructed using different constructors, both are considered instances of F
and G
. The reason is F
and G
both have the same prototype.
So, is the constructor useful in any sense?
Apart from those two, no more uses come to mind.
Why all of this relates to my problem?
As pointed out in other answers, Array() is the constructor of [] and the prototype is non-writable so you're not going to have any problem performing that check in the same window/frame.
However, there's a caveat. If your webapp uses more than one window or frame you have to take into account that each window or frame is a distinct execution context, and each has its own global object and its own set of constructor functions. Two arrays created in two different frames inherit from two identical but distinct prototype objects, and an array created in one frame is not instanceof the Array() constructor of another frame.
Upvotes: 0
Reputation: 94
This topic is already documented on W3C JavaScript Array constructor Property:
The constructor property returns an array's constructor function:
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.constructor;
The constructor property returns:
function Array() { [native code] }
As you can see Array()
is the constructor.
And yes, that means that the purpose of the []
literal and Array()
constructor are basically the same, although the differences are already pointed out in this StackOverflow post.
Upvotes: 2
Reputation: 8333
Yes, [1,2]
is equal to new Array(1,2)
. As per the Mozilla documentation:
Syntax
[element0, element1, ..., elementN]
new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)
Upvotes: 2