[go: up one dir, main page]

0% found this document useful (0 votes)
21 views14 pages

Prototype Inheritance

The document discusses JavaScript prototypes and how they allow objects to inherit properties from other objects. It explains that every object has an internal prototype reference that gives it structure and allows it to inherit properties. It provides examples of how prototype inheritance works when creating objects with constructor functions.

Uploaded by

dk1078451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views14 pages

Prototype Inheritance

The document discusses JavaScript prototypes and how they allow objects to inherit properties from other objects. It explains that every object has an internal prototype reference that gives it structure and allows it to inherit properties. It provides examples of how prototype inheritance works when creating objects with constructor functions.

Uploaded by

dk1078451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Prototype

Every object has an internal prototype that gives it its structure.


This internal prototype is a reference to an object describing the
code and data that all objects of that same type will have in
common.
Prototype Object
Every object is associated with another Object in JavaScript.
A
Object Properties
A is prototype Object of Object B

B B will inherit All properties of Prototype Object A


Object Properties
Prototype Object
Every object is associated with another Object in JavaScript.
var b = {};

Object.prototype
Object Properties
Object.prototype is prototype Object of Object B

B
Object Properties
B will inherit All properties of Object.prototype

Note - Prototype Object of Object.prototype is null


Prototype Object
Every object is associated with another Object in JavaScript.
var b1 = new Object( );
Constructor Function

Prototype property of the Constructor Function


Object.prototype
Object which is in this case ‘Object.prototype’ is prototype
Properties
Object of Object B1

B1
Object B1 will inherit All properties of Object.prototype
Properties

Note - Prototype Object of Object.prototype is null


Prototype Object
Every object is associated with another Object in JavaScript.
var b2 = new Array( );
Constructor

Prototype property of the Constructor Function


Array.prototype
Object which is in this case ‘Array.prototype’ is prototype
Properties
Object of Object B2

B2
Object B2 will inherit All properties of Array.prototype
Properties

Note - Prototype Object of Array.prototype is Object.prototype and


Prototype Object of Object.prototype is null
Prototype Object
Null

Object Object.prototype

Array.prototype Array RegExp b

b2
prototype is the property of function which points to the prototype object. Prototype Object
Prototype object can be accessed using Function_Name.prototype

function Mobile( ) { Function Prototype


}
prototype

var lg = new Mobile()


var g = new Mobile() Object Object

__proto__ __proto__

When you create a new object of that


function using new keyword JS Engine lg.__proto__ === Mobile.prototype
creates an object and sets a property named
__proto__ which points to its function’s
prototype object
function Mobile( ) { Function Prototype

}
prototype

var lg = new Mobile()


Object // undefined
lg.a
__proto__

I am looking for a
function Mobile( ) { Function Prototype
this.a = 10
}
prototype

var lg = new Mobile()


Object
lg.a
__proto__

// 10
I am looking for a
function Mobile( ) { Function Prototype

}
prototype
Mobile.prototype.a = 10

var lg = new Mobile()


Object // 10
lg.a
__proto__

I am looking for a
Mobile.prototype.a === lg.__proto__.a
function Mobile( ) { Function Prototype
this.a = 10
} prototype
Mobile.prototype.a = 10
var lg = new Mobile()

Object
lg.a
__proto__

// 10
I am looking for a
Object.prototype

__proto__ null

function Mobile( ) { Mobile Function Mobile.Prototype


this.a = 10;
} prototype __proto__

Mobile.prototype.z = 30
var m = new Mobile() m Object
__proto__

function Samsung( ) { Samsung Function Samsung.Prototype


Mobile.call(this);
prototype __proto__
this.b = 20;
}

s Object
var s = new Samsung()
__proto__
Object.prototype

__proto__ null

function Mobile( ) { Mobile Function Mobile.Prototype


this.a = 10;
} prototype __proto__

Mobile.prototype.z = 30
var m = new Mobile() m Object
__proto__

function Samsung( ) { Samsung Function Samsung.Prototype


Mobile.call(this);
prototype __proto__
this.b = 20;
}

s Object
var s = new Samsung()
__proto__
Object.prototype

__proto__ null

function Mobile( ) { Mobile Function Mobile.Prototype


this.a = 10;
} prototype __proto__

Mobile.prototype.z = 30
var m = new Mobile() m Object
__proto__

function Samsung( ) { Samsung Function Samsung.Prototype


Mobile.call(this);
prototype __proto__
this.b = 20;
}

s Object Samsung.prototype = Object.create(Mobile.prototype)


var s = new Samsung()
__proto__

You might also like