wenjun.yan
wenjun.yan

Reputation: 618

What is a constructor function's prototype?

Say, I have a constructor function called MyClass. And I create an object obj out of it. obj inherits from MyClass.prototype. So here is my question:

Where is the MyClass.prototype from? Is it just a plain object with a constructor property?

Thanks for your answers.

// Constructor
function MyClass() {
}

var obj = new MyClass;

// object inherits from MyClass.prototype
obj.__proto__ == MyClass.prototype;
// => true

// MyClass.prototype inherits from Object.prototype
MyClass.prototype.__proto__ == Object.prototype;
// => true

Upvotes: 0

Views: 62

Answers (2)

Bergi
Bergi

Reputation: 665574

Where is the MyClass.prototype from?

It's implicitly created when the function object (MyClass) is created.

Is it just a plain object with a constructor property?

Yes, exactly. No more than that, no magic involved :-)

Upvotes: 2

A.B
A.B

Reputation: 20455

  • MyClass.prototype is simply a object having constructor method(property).
  • as soon as you create object with new operator a protype is also created and the constructor function being called with new operator becomes its property/method

Upvotes: 0

Related Questions