JavaScript

OOP1: What is prototype?

Prototype is a property that will be automatically created once you create a new function. And this property points to an object that contains shared properties and methods. And in this object, a property named constructor will be automatically added and points to the new function which is important for checking object type.

Person.prototype.constructor//points to Person

After created the constructor function Person,  Person.prototype will  contain the constructor property and  also inherit other methods from Object. Once an instance is created, it will automatically create an inaccessible property named _proto_ and points to the constructor function’s prototype.

var person = new Person();
//person's _proto_ property points to Person.prototype
Person.prototype.isPrototypeOf(person1);
//Object.getPrototypeOf(person1) both of them can prove

Person also contains an inaccessible _proto_ property as you see Person is also an instance of Function. This time _proto_ points to Function.prototype, and Function is also an instance of Object. So its _proto_ points to Object.prototype, in the end Object.prototype points to null, you may take it as global object, here it can be window.

So the _proto_ forms a chain and that is the prototype chain, or it can be taken as an inherit chain. From the very top Object, many shared properties and methods are passed to the very instance and that’s wonderful.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s