Online Book Reader

Home Category

Getting Good with JavaScript - Andrew Burgess [22]

By Root 230 0
or parent object, of every object made with a given constructor function is stored at ConstructorFunction.prototype. All the methods and properties of this object are accessible from the "children" objects.

This is why it's called "Prototypal Inheritance"; each object inherits from the same prototype. It's important to note that if a given child object has a given property of it's own—say, my_obj.name—the prototype won't be checked for the property. Also, note that when you change a property or method from an object, you're changing the property on that object, not the prototype object.

Let's look at an example, just to cement this down:

Example 4.9


function Product(name) {

if (name) {

this.name = name;

}

}

Product.prototype.name = "To be announced";

Product.prototype.rating = 3;

var ipad = new Product("iPad");

alert( ipad.name ); // "iPad";

alert( ipad.rating ); // 3


Here, we've created a simple Product. Right now, you can see that the name property is right on the object ipad and the rating property is being inherited from its prototype.

Example 4.10


// ipad from above

ipad.rating = 4;

ipad.rating; // 4

Product.prototype.rating; // 3


By changing the rating, we're actually adding a rating parameter to the ipad object. Since it has its own property called rating now, when we ask for ipad.rating, it no longer goes to the prototype … however, the prototype's value for rating hasn't been changed.

Object Methods


We discussed methods for every type except Objects at the end of the last chapter. This was for two reasons:

I wanted to keep all object-related material together, right here.

Most of the methods you'll use on objects will be ones you create yourself.

However, there are several useful methods that are built into objects. Since every object you create is an Object (i.e, inherits from Object.prototype), these methods are stored there.

ROCKSTAR TIP

This revelation might get you wondering where the methods for, say, strings, are stored. Well, there is a String object, which has a prototype property. That's where they are kept. Some string methods are also object methods; since everything is an object—meaning that String inherits from Object.prototype, too—String doesn't need to have its own versions of those methods, except when they are different from the Object versions.

hasOwnProperty

We talked about using a for-in loop to iterate over arrays in the last chapter; you can use this on objects, too.

Example 4.11


function Person(name) {

this.name = name;

}

Person.prototype.legs = 2;

var person = new Person("Joe"), prop;

for (prop in person) {

console.log(prop + ": " + person[prop]);

}

// in console:

// name : Joe

// legs: 2


However, there's something to be aware of here. If your object is inheriting from a prototype with properties, those properties will be looped over as well. This might make for-in cough up results you didn't want. In this case, objects have a handy method for determining if a given property belongs to the object or its prototype: it's called hasOwnProperty:

Example 4.12


function Person(name) {

this.name = name;

}

Person.prototype.legs = 2;

var person = new Person("Joe"), prop;

for (prop in person) {

if (person.hasOwnProperty(prop)) {

console.log(prop + ": " + person[prop]);

}

}

// console:

// name: Joe


It's a bit inefficient to have to write that inner if block every time you want to loop over an object, but get used to it; it's a best practice.

toString

Every object has a toString method. This method is called when the object is to be printed out anywhere; it converts it to a string. The different built-in types do different things: strings obviously don't change; numbers become strings of numbers; dates become nicely formated date strings. But what about general objects?

Example 4.13


var o = { name : "Andrew" };

alert( o.toString()); // "[object Object]"


This is pretty useless, because it tells you absolutely nothing about the object, other than that it is indeed an object. Use this as a reminder

Return Main Page Previous Page Next Page

®Online Book Reader