Prototype in javascript

Something Like that:
Prototype

For that you need to create first html code.

HTML

<p id="demo1"></p>
<p id="demo2"></p>

After that add this JS code.

JS

var person = {
	firstname: 'Default',
	lastname: 'Default',
	getFullName: function(){
		return this.firstname + ' ' + this.lastname;
	}
}

var john = {
	firstname: 'John',
	lastname: 'Doe'
}

john.__proto__ = person;

document.getElementById('demo1').innerHTML = john.getFullName();
document.getElementById('demo2').innerHTML = john.firstname;

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”OjvBBY” default_tab=”result” user=”pradeepanvi”]See the Pen Prototype in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

John Doe
John

HTML

<p id="demo1"></p>

After that add this JS code.

JS

var person = {
	firstname: 'Default',
	lastname: 'Default',
	getFullName: function(){
		return this.firstname + ' ' + this.lastname;
	}
}

var jane = {
	firstname: 'Jane'
}

jane.__proto__ = person;
document.getElementById('demo1').innerHTML = jane.getFullName();

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”VzXEOE” default_tab=”result” user=”pradeepanvi”]See the Pen Prototype in javascript 02 by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

Jane Default

Everything is an Object (Or A Primitive)

For that you need to create first html code.

HTML

<p>What is a = <span id="demo1"></span></p>
<p>What is b = <span id="demo2"></span></p>
<p>What is c = <span id="demo3"></span></p>

After that add this JS code.

JS

var a = {};
var b = function() {};
var c = [];
document.getElementById('demo1').innerHTML = a.__proto__;
document.getElementById('demo2').innerHTML = b.__proto__;
document.getElementById('demo3').innerHTML = c.__proto__;

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”OjvBBY” default_tab=”result” user=”pradeepanvi”]See the Pen Prototype in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

What is a = [object Object]
What is b = function () { [native code] }
What is c =

Above you can see a is object and b is function
Now we will function is also object.

JS

document.getElementById('demo1').innerHTML = a.__proto__;
document.getElementById('demo2').innerHTML = b.__proto__.__proto__;
document.getElementById('demo3').innerHTML = c.__proto__.__proto__;

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”NvYmvY” default_tab=”result” user=”pradeepanvi”]See the Pen Prototype in javascript 04 by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

What is a = [object Object]
What is b = [object Object]
What is c = [object Object]

Leave a Reply

Your email address will not be published. Required fields are marked *