NodeLiveLessons icon indicating copy to clipboard operation
NodeLiveLessons copied to clipboard

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

Open nimitbhandari13 opened this issue 7 years ago • 0 comments

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

nimitbhandari13 avatar May 26 '18 00:05 nimitbhandari13