Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript prototype (Prototype Object)

JavaScript prototype (Prototype Object)

In JavaScript, the prototype is a very important concept. It provides a mechanism for inheritance and shared properties among objects. Every JavaScript object has an associated prototype object, through which properties and methods can be shared, reducing memory usage.

All JavaScript objects inherit properties and methods from a prototype object.


object's proto property

Every JavaScript object (except null) automatically has a hidden property proto, which points to the object's prototype. This proto is the key to implementing inheritance:

let obj = {};

console.log(obj.__proto__); // 输出: [object Object], 即 obj 的原型是 Object.prototype


Constructors and prototypes

In earlier chapters we learned how to use an object's constructor:

Example

function

Person

(

first

,

last

,

age

,

eyecolor

)

{

this

.

firstName

=

first

;

this

.

lastName

=

last

;

this

.

age

=

age

;

this

.

eyeColor

=

eyecolor

;

}

var

myFather

=

new

Person

(

"

John

"

,

"

Doe

"

,

50

,

"

blue

"

)

;

var

myMother

=

new

Person

(

"

Sally

"

,

"

Rally

"

,

48

,

"

green

"

)

;

Try it »

We also know that you cannot add new properties to an object that already has a constructor:

Example

Person

.

nationality

=

"

English

"

;

Try it »

To add a new property, you need to add it inside the constructor function:

Example

function

Person

(

first

,

last

,

age

,

eyecolor

)

{

this

.

firstName

=

first

;

this

.

lastName

=

last

;

this

.

age

=

age

;

this

.

eyeColor

=

eyecolor

;

this

.

nationality

=

"

English

"

;

}

Try it »

When you create an object with a constructor function, the constructor's prototype property becomes the prototype of all instances created by that constructor.

Example

function

Person

(

name

)

{

   

this

.

name

=

name

;

}

Person.

prototype

.

sayHello

=

function

(

)

{

    console.

log

(

"Hello, my name is "

this

.

name

)

;

}

;

let alice

=

new

Person

(

"Alice"

)

;

alice.

sayHello

(

)

;

// Output: Hello, my name is Alice

In this example, Person.prototype is the prototype of the alice object, so alice can access the sayHello method.


The prototype chain

In JavaScript, objects achieve inheritance through the prototype chain. When an object tries to access a property or method, JavaScript first checks whether the object itself has it. If not, it searches up the prototype chain.

let obj = {};

console.log(obj.toString()); // 输出: [object Object]

// This `toString` method is actually inherited from `Object.prototype`

In the example above, the obj object does not define a toString method, so JavaScript searches up the prototype chain and finally finds the method in Object.prototype.


Modifying the prototype

You can dynamically modify an object's prototype, which affects all objects created from that prototype:

Example

function

Person

(

name

)

{

   

this

.

name

=

name

;

}

Person.

prototype

.

sayHello

=

function

(

)

{

    console.

log

(

"Hello, my name is "

this

.

name

)

;

}

;

let bob

=

new

Person

(

"Bob"

)

;

bob.

sayHello

(

)

;

// Output: Hello, my name is Bob

// Modify the prototype

Person.

prototype

.

sayGoodbye

=

function

(

)

{

    console.

log

(

"Goodbye from "

this

.

name

)

;

}

;

bob.

sayGoodbye

(

)

;

// Output: Goodbye from Bob

In this example, we add a new method sayGoodbye to Person.prototype, and the bob object can immediately access this new method.


The Object.create method

The Object.create method lets you create a new object and set its prototype to a specified object.

Example

let personPrototype

=

{

    sayHello

:

function

(

)

{

        console.

log

(

"Hello, my name is "

this

.

name

)

;

   

}

}

;

let alice

=

Object

.

create

(

personPrototype

)

;

alice.

name

=

"Alice"

;

alice.

sayHello

(

)

;

// Output: Hello, my name is Alice

In this example, alice's prototype is personPrototype, so the alice object can access the sayHello method.


prototype inheritance

All JavaScript objects inherit properties and methods from a prototype object:

All objects in JavaScript are instances of Object, which sits at the top of the prototype chain.

A JavaScript object has a chain pointing to a prototype object. When trying to access a property of an object, JavaScript searches not only the object itself but also the object's prototype, and the prototype's prototype, continuing up the chain until it finds a matching property or reaches the end of the prototype chain.

Date Object, Array Object, and Person An object inherits from Object.prototype its prototype.

Adding properties and methods

Sometimes we want to add new properties or methods to all existing objects.

Other times we want to add properties or methods in an object's constructor.

Using the prototype property, you can add new properties to an object's constructor:

Example

function

Person

(

first

,

last

,

age

,

eyecolor

)

{

this

.

firstName

=

first

;

this

.

lastName

=

last

;

this

.

age

=

age

;

this

.

eyeColor

=

eyecolor

;

}

Person

.

prototype

.

nationality

=

"

English

"

;

Try it »

Of course, we can also use the prototype property to add new methods to an object's constructor:

Example

function

Person

(

first

,

last

,

age

,

eyecolor

)

{

this

.

firstName

=

first

;

this

.

lastName

=

last

;

this

.

age

=

age

;

this

.

eyeColor

=

eyecolor

;

}

Person

.

prototype

.

name

=

function

(

)

{

return

this

.

firstName

"

"

this

.

lastName

;

}

;

Try it »