The confusion arises when people talk about constructor and prototype simultaneously.
In the following example:
var employee = function Emp(name) {
this.name = name;
}
var jack = new employee("Jack Dwain");
employee.constructor //gives Function()
employee.prototype // gives Emp {}
employee.prototype.constructor //gives Emp(name)
jack.constructor //gives Emp(name)
jack.prototype //gives undefined
In the following paragraphs we will be explaining why those results make sense and everything will be better understandable after explaining the different concepts of them.
Its a pretty hard thing to wrap your mind around if you are used to the ease of extending objects in other OOP languages, but i'll do my best to explain the uses of those and what is what. I am going to assume you are familiar with other OOP languages.
All functions have the prototype Function(). They are inheriting all base functionlity from Function like toString() and valueOf().
Then there is a constructor. That is what you use to initialise an object with.
p = new Foo();
So in this case we have two things.
- A function
Foo
withFunction
as prototype(Foo) - A
Function
object withFoo()
as constructor(p)
(following me yet?)
The Foo()
constructor can override some base functionality of the Function constructor, but also leave it as it is and make good use of it.
If you are familiar with OOP priciples, The prototype is the base class, the constructor your current class. in OOP the above would be:
class Foo extends Function
Knowing this, let's start answer why the results of the code above:
employee.constructor //gives Function()
In JavaScript functions are also objects, which can be constructed using its own constructor which is Function
. So you can write following code to get a instance of Function
.
var employee2 = new Function('a', 'b', 'return a+b');
Same happens when you create function using function literal like our example. The constructor property of this object also refers to the same native Function object/class.
employee.prototype // gives Emp {}
Each object in JavaScript has a prototype associated with it. Though only function objects prototype is directly accessible with the .prototype
. This same prototype is copied on its objects prototype when you create objects with new
keyword. Primarily this copying is responsible for the inheritance/extension. Although the prototype is copied, it is not directly accessible like in case of Function
objects. It's available in non standard way with .__proto__
. Following code will return true.
jack.__proto__==employee.prototype
employee.prototype.constructor //gives Emp(name)
As said in the documentation of Object.prototype.constructor
. This returns a reference to the Object
function that created the instance's prototype. Here the object being refered is employee
.prototype and not employee. This is bit complex but prototype of object employee.prototype was created by the function Emp(name)
jack.constructor //gives Emp(name)
As said in the previous point, this objects prototype was created by function Emp(name)
when you created the object using new Emp()
,
jack.prototype //gives undefined
jack is not a function object, so you cant access its prototype like that. You can access(not a standard way) the prototype of jack like following.
jack.__proto__