var Foo = new function(){
this.A = 1;
this.B = 2;
};
var Bar = {
A: 1,
B: 2
};
typeof Foo === "object"
typeof Bar === "object"
What's the difference between the two?
I only found their __proto__
objects to be different:
var Foo = new function(){
this.A = 1;
this.B = 2;
};
var Bar = {
A: 1,
B: 2
};
typeof Foo === "object"
typeof Bar === "object"
What's the difference between the two?
I only found their __proto__
objects to be different:
OK:
var Foo = new function(){
this.A = 1;
this.B = 2;
};
We've got var Foo = new
a function — which means that the runtime will invoke that function with this
set to a newly-constructed object whose internal prototype link will be set to the value of the "prototype" property of that function. Because that's just an anonymous function instantiated right there in the initialization, its "prototype" property is just a plain empty object, so it really doesn't do anything. The constructor function does initialize two properties of the new object, and that new object is the returned value from the new
expression. Thus, Foo
is assigned a reference to that constructed object, and the object has a couple of assigned properties. It also has a prototype chain that includes the empty object from the anonymous constructor function, and then after that the prototype inherited from the Object constructor.
In this:
var Bar = {
A: 1,
B: 2
};
we have a variable (Bar
) being set to the result of an object initializer expression. That always creates a plain object that inherits only from the Object prototype. The only way that this object is different from the Foo
object is that the Foo
object has an extra (empty) object in its prototype chain.
So:
Foo
will end up being a plain object, not a function.Bar
will also be a plain object, more obviously not a function.Foo
and Bar
will be similar but not identical (well, two different objects are never identical, but I mean in terms of their particular characteristics) because Foo
has that extra empty object in its prototype chain.The first one is a function constructor which outputs a function object (what is assigned to Foo
), and the other is just a plain javascript object.
In Javascript all functions are objects. And in your case Foo and Bar are the same, but you can construct the first.
On the same token, I would say that the key insight with constructor functions in this case is that if we have:
function myFoo (){
this.A = 1;
this.B = 2;
};
then
var Foo1 = new myFoo();
the this context is Foo.
var Foo2 = myFoo();
the this is bound to the current context, the window object in this case (meaning you actually have window.A = 1).
See the MDN documentation for a more extensive description.
First note it's irrelevant that you omitted the parentheses when calling your anonymous function with new
. JSLint will yell at you because it's confusing to do this, but that's what's going on.
I'm pretty sure the internal prototype is the only difference. It may be easier to write this out as it's normally used.
var Car = function() {
this.seats = 4;
};
var car1 = new Car();
var car2 = {
seats: 4
};
Reading this it's clear to me that car1
is an object whose internal prototype is Car
- which in your case is an anonymous function not directly held by a variable - and car2
is an object whose internal prototype is Object.