Skip to content

Chapter 2 Prototypal Inheritance doesn't work with the area code #5

@nimitbhandari13

Description

@nimitbhandari13

Hi Marc,

When I try to use the prototypal inheritance lecture and create the same Shape vs Square class and run the following code :

function Shape() {
}

Shape.prototype.X = 0;
Shape.prototype.Y = 0;

Shape.prototype.move = (x,y ) => {
this.X = x;
this.Y = y;
}

Shape.prototype.distance_from_origin = () => {
return Math.sqrt( this.X * this.Y + this.Y*this.Y );
}

Shape.prototype.area = () => {
throw new Error( " Need a 2d form" );
}

var s = new Shape();
s.move( 10, 10 );
console.log( s.distance_from_origin() );

function Square() {
}

Square.prototype = new Shape();
Square.prototype.proto = Shape.prototype;

Square.prototype.Width = 0;

Square.prototype.area = () => {
return ( this.Width * this.Width );
}

var sq = new Square();
sq.move( 5, 5 );
sq.Width = 10;
console.log( sq.Width );
console.log( sq.distance_from_origin() );
console.log( sq.area() );

The value of sq.area() always gets returned as NaN, even when the console.log( sq.Width ) prints the correct value. Can you highlight what might be going on here ?

Thanks,
Nimit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions