Reputation: 451
A prototype is an object and every function you create automatically gets a prototype property that points to a new blank object. This object is almost identical to an object created with an object literal or Object() constructor, except that its constructor property points to the function you create and not to the built-in Object().
Above is an excerpt from Stoyan Stefanov's JavaScript patterns book.
var Ctor = function(msg) {
this.msg = msg;
this.print = function() {
console.log(this.msg);
}
};
So, I understand that the Ctor
will have a prototype
property, which I can get access to. And to check where the constructor property will point to, I can do:
Ctor.prototype.constructor
If I do this on a console, I get the pointer (?) to function Ctor(msg)
So far so good.
Now I am trying to create a variable through Object()
constructor and see where its constructor property points to:
var CtorCopy = new Object(Ctor);
CtorCopy.prototype.constructor;
Now I see that the result of this on the console is function Ctor(msg)
just as the previous case.
So, which gets me to conclude that the constructor property of the object created through Object()
doesn't point to built-in Object() (Actually, I am not sure what built-in Object() means) but to the function I created.
This is confusing.
Upvotes: 2
Views: 69
Reputation: 15293
The above quote in your question is just saying that whenever you create a function that it will have a prototype property attached to it which points to an object, which in it's nature is almost the same as an object created with the build in Object function or literal.
var obj = {};
// or
var obj = new Object();
The difference is just, that its constructor property points to your Ctor function and not to the build in Object function. You can see the difference if you do the following.
var Ctor = function(msg) {
this.msg = msg;
this.print = function() {
console.log(this.msg);
}
};
console.log(typeof Ctor.prototype) // object
So prototype is an object like this one.
console.log(typeof {}); // object
But its constructor property will point to your function,...
console.log(Ctor.prototype.constructort); // function Ctor()
where as the following will point to the build in Object function.
var obj = {};
console.log(obj.constructor); // function Object()
Since all objects are descended from Object, they all inherit methods and properties form the Object.prototype. So if you would want to know which prototype was used to instantiate your Ctor object you could do
console.log(Ctor.prototype.__proto__); // Object {}
which will give you the build in object.
Which will explain why the following returns true.
console.log(Ctor instanceof Object); // true
So if you are using new Object to create an object of your Ctor function, which is nothing but an object and which pretty much is the same as doing this
var CtorCopy = {};
CtorCopy = Ctor;
It will eventually have a prototype property with a constructor pointing to your Ctor object. I hope this will makes it a little bit more clear on what is meant with the build in object. Since you've mentioned that you are not sure what build in object means in this case.
Upvotes: 0
Reputation: 197
The text that you quote at the start of your question is referring to the difference between creating an object via a constructor function you've defined and using something like:
var obj = new Object();
// or
var obj = {};
By passing in a value to Object you are invoking a different behaviour which causes it create an object of that Type. According to MDN:
The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value.
So essentially you've managed to do the same thing just with more code in the same way as my examples above. See here - MDN.
Upvotes: 2
Reputation: 12131
According to MDN (emphasis mine):
The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value.
Which means that if you pass your Ctor
function (which is an object) to the Object
constructor, it will simply return the original function back to you. You are looking at two identical objects.
Upvotes: 1