0

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()?

LostMyGlasses
  • 3,074
  • 20
  • 28
b Tech
  • 412
  • 4
  • 14
  • 1
    `Array` is constructor. `[]` shortcut for `new Array()` – Maxx Oct 06 '16 at 08:52
  • 1
    @Maxx Not really. – Bergi Oct 09 '16 at 16:46
  • Don't listen to W3schools. That sentence is complete rubbish. `instanceof` does only care from which prototype an object does inherit, not be which function it was constructed. – Bergi Oct 09 '16 at 16:47
  • @Bergi what is your explanation? – Maxx Oct 10 '16 at 06:54
  • @Maxx Literals just create arrays/objects using native functions. They do not invoke `new Array` or `new Object` nor any setters. The difference is important when the global `Array`/`Object` identifiers are overwritten. – Bergi Oct 10 '16 at 10:07

3 Answers3

2

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)
mrks
  • 8,033
  • 1
  • 33
  • 62
2

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.

Community
  • 1
  • 1
ther
  • 84
  • 4
0

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?

  1. It could be thought of as the "public identity" of the class (as you can see, instanceof reinforces this fact) though prototypes are the fundamental identity.
  2. Can be used as a reference to the constructor function that created the object (though you'd have to be careful and set it up correctly).

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.

acontell
  • 6,792
  • 1
  • 19
  • 32
  • AFAIK `Array`/`Object` literals are internally constructed via `Array`/`Object`. Pointing out that constructors in Javascript are just fakes is still right and it is really hard to find some valid reasons for their usage (as you've tried). Anyway, you don't answer the OP's question but +1 for "unmaksing" the uselessness of constructors. –  Oct 06 '16 at 10:17