Now it was easy to handle Objects by PROTOTYPE Javascript concept…..

vijiayoti

Definition:

The prototype property allows you to add properties and methods to an object.

How to use prototype in JAVASCRIPT…..here some sample code:

Code:


function mathfn(a,b) {
this.a = a;
this.b = b;
mathfn.prototype.add=function(){
return this.a+this.b;
}
mathfn.prototype.sub = function () {
return this.a-this.b;
}
}
window.onload=function(){
var obj1=new mathfn(5,6);
var obj2=new mathfn(76,36);
console.log(obj1.add());
console.log(obj2.add());
console.log(obj1.sub());
console.log(obj2.sub());
}

Explanation:

Here mathfnas Constructor………

I am getting answer in console.log.

Simply we can create many instance having single prototype object.

Note:

  • The prototype of an object provides access to members of that object.
  • The keyword this refers to the object context within which the function is executing.
  • The language is functional, i.e. everything is Object, including functions, and functions are values.

View original post

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s