Skip navigation

To understand the  constructor, consider the following code

// constructor function definition
function sum(a,b){
    this.total=0;
    this.total = a + b;
    return this.total;
}

// create object using sum constructor
var sal = new sum(3,3);

alert(sal.total);  // 6
alert(sal.constructor)  // displays function definition above

Now sal is object of type sum.

Interesting part is you can create sum object using sal though constructor property. Here is how…

var sal1 = new sal.constructor(5,5);
alert(sal1.total);  // 10
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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: