1

I'm new to javascript.

I'm writing an object oriented program to find area of a rectangle and a square. I have three classes - Shape, Square and Rectangle. I would like to inherit parent class - Shape, into the child class - Square, I ran into a problem related to prototypes. Code:

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

I would like to know:
1. Whether writing Square.prototype= new Shape(); would suffice in terms of inheriting the class Shape into class Square?
2. What difference does the line:
Square.prototype.__proto__= Shape.prototype; cause to class Square.

Any help is much appreciated, thanks!

Oriol
  • 274,082
  • 63
  • 437
  • 513
Vijay Singh
  • 277
  • 1
  • 3
  • 15
  • 2
    See [How does JavaScript .prototype work?](http://stackoverflow.com/q/572897/1529630) – Oriol Nov 25 '14 at 22:47
  • 2
    1) Yes. 2) Nothing at all (because `Square.prototype.__proto__` already points to `Shape.prototype`). However [there are better ways to establish inheritance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance). – Felix Kling Nov 25 '14 at 22:50
  • 1
    seems impure to construct an instance of Shape to define a class for Square; how are you supposed to pass arguments to the Shape creation? in short, it feels like another lang more than JS – dandavis Nov 25 '14 at 22:55
  • @dandavis: you are correct, the program is actually a nodejs program, does that change everything? Btw, I intend to prototypically add properties to class Square. Felix and Oriol, thanks a lot for your comments! – Vijay Singh Nov 25 '14 at 23:03
  • fwiw, i find it easier to put methods together on a single object instance that can then affect any of my other instances; why does every object need it's own methods anyway? for things like smalltalk it makes sense, but for a scripting lang like JS with call/bind/apply/defineProperty not so much. it also makes serialization and expansion easier if data objects contain only "own properties", and you don't need to worry about inheritance or re-creating inheritance to persist state. since you don't inherit data, it makes everything clean, and V8 uses objectObjects faster than custom types. – dandavis Nov 25 '14 at 23:11
  • Answer to a similar question: http://stackoverflow.com/questions/5398487/confusion-about-setting-something-prototype-proto – Vijay Singh Nov 25 '14 at 23:18
  • As Felix and Dan already commented; use Object.create to set prototype part of inheritance: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 25 '14 at 23:50

1 Answers1

0

For your inheritance, I would rather use:

Square.prototype = Object.create(Shape.prototype);
Square.prototype.constructor = Shape;

For these kind of reasons: Understanding the difference between Object.create() and new SomeFunction()

Square.prototype.__proto__= Shape.prototype; is not a good practice because it will replace all the prototype chain of your object with another prototype. This operation is very slow. Moreover the property __proto__ is deprecated and could disappear at any time: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

Community
  • 1
  • 1
Gnucki
  • 5,043
  • 2
  • 29
  • 44