Thursday, 19 January 2012

Javascript Prototyping:-

Prototyping Is The Great Feature In Javascript Which Is Used To Create Propertys And Methods
If We Add The Property Or Method Using Prototyping It Added To All The Objects.

var person1 = new per('abc', '123');
var person2 = new per('xyz', '456');

function per(name, id)
   /*Here This Is Compulsary To Represent Current Calling Object*/ = name; = id;

/*Here We Are Adding The Property Called Address Using Prototyping*/
per.prototype.address = "mumbai";

/*Here We Are Adding The Property Called PinCode Using NormalWay */
person1.pinCode = 507164;

/*The Difference Between Above Two Is The Address Is Added To All The Objects(To The person1 & person2 Objects) And PinCode Is Added To Only To That person1 Object.(Not To The person2 Object)*/

/*HasOwnProperty Is Used To Know Either The Property Belongs To That Object Or Not*/
person1.hasOwnProperty('name'); /*true*/
person1.hasOwnProperty('id'); /*true*/
person1.hasOwnProperty('address'); /*false becuase it added using prototyping*/
person1.hasOwnProperty('pinCode'); /*true*/

/*Here We Are Adding The Method Called getNameLength Using Prototyping*/
per.prototype.getNameLength = function () {

/*Calling The Function Which Is Added By Using The Prototyping*/
var len = person1.getNameLength(); 

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…

No comments:

Post a Comment