JavaScript Instance Properties

A constructor that is created in JavaScript can have properties defined for it. Each instantiation of the constructor has it’s own copy of the properties. These are called ‘instance properties.’

For example, suppose the following constructor was defined:

function Dog()
{
this.legs = 4;
}

From this constructor, two instances of the Dog class could be instantiated:

var myDog = new Dog();
var myDog2 = new Dog();

Each of these instances has the same value for the ‘legs’ property but they eahch have their own copy.

document.write(myDog.legs);
document.write(myDog2.legs);

Related articles:
Object-oriented JavaScript
JavaScript Instance Methods

Advertisement

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.