Only the else part of the if statement is executing, it is showing the selector value.

That's good, your updated code must be doing something else than what has already been covered, because when I follow what you have stated, my web browser still shows "Uncaught ReferenceError: selector is not defined"

Could you put up your code at jsfiddle.net so that we can get a better idea of how things currently are?

That is correct. That's one of the causes of the problems you are facing

shippuuden
—
2011-12-23T08:28:02Z —
#17

ok, so what would be the best way to do this?

I want All_rounded_corners_one_element to inherit Custom_rounded_container.

Selector and color needs to be inside Custom_rounded_container because other objects will also inherit Custom_rounded_container.

The book I am following seems to be wrong then.

This is the code in the JavaScript: The Definitive Guide, 5th Edition text book by David Flanagan

// Here is a simple Rectangle class.
// It has a width and height and can compute its own area
function Rectangle(w, h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function( ) { return this.width * this.height; }
// Here is how we might subclass it
function PositionedRectangle(x, y, w, h) {
// First, invoke the superclass constructor on the new object
// so that it can initialize the width and height.
// We use the call method so that we invoke the constructor as a
// method of the object to be initialized.
// This is called constructor chaining.
Rectangle.call(this, w, h);
// Now store the position of the upper-left corner of the rectangle
this.x = x;
this.y = y;
}
// If we use the default prototype object that is created when we
// define the PositionedRectangle( ) constructor, we get a subclass of Object.
// To subclass Rectangle, we must explicitly create our prototype object.
PositionedRectangle.prototype = new Rectangle( );
// We create this prototype object for inheritance purposes, but we
// don't actually want to inherit the width and height properties that
// each Rectangle object has, so delete them from the prototype.
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
// Since the prototype object was created with the Rectangle( ) constructor,
// it has a constructor property that refers to that constructor. But
// we want PositionedRectangle objects to have a different constructor
// property, so we've got to reassign this default constructor property.
PositionedRectangle.prototype.constructor = PositionedRectangle;
// Now that we've configured the prototype object for our subclass,
// we can add instance methods to it.
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width &&
y > this.y && y < this.y + this.height);
}
var r = new PositionedRectangle(2,2,2,2);
print(r.contains(3,3)); // invoke an instance method
print(r.area( )); // invoke an inherited instance method
// Use the instance fields of the class:
print(r.x + ", " + r.y + ", " + r.width + ", " + r.height);
// Our object is an instance of all 3 of these classes
print(r instanceof PositionedRectangle &&
r instanceof Rectangle &&
r instanceof Object);
// Store a reference to our superclass constructor.
PositionedRectangle.prototype.superclass = Rectangle;
With this property defined, the syntax for constructor chaining is simpler:
function PositionedRectangle(x, y, w, h) {
this.superclass(w,h);
this.x = x;
this.y = y;
}

I based my code on the sample code in the text book.

Paul_Wilkins
—
2011-12-23T08:29:58Z —
#18

shippuuden said:

The book I am following seems to be wrong then.

What I notice that seems to be different from what you show from the book, is the second line here.

If I understood the text book correctly, if I want to subclass the parent, I must explicitly create the prototype object.

The parent being Custom_rounded_container.

From the textbook:

// If we use the default prototype object that is created when we// define the PositionedRectangle( ) constructor, we get a subclass of Object.// To subclass Rectangle, we must explicitly create our prototype object.

PositionedRectangle.prototype = new Rectangle( );

Paul_Wilkins
—
2011-12-23T08:43:13Z —
#20

shippuuden said:

If I understood the text book correctly, if I want to subclass the parent, I must explicitly create the prototype object.

Notice in the Rectangle constructor how even if the values are undefined which they will be when called with no arguments) that no errors will be thrown since the values being assigned to the object will just be undefined?