0

See line 25 in Mozilla's Guide. It says that it's wrong to use code equivalent to

SubType.prototype = new SuperType();

I sort of get the idea that you can't call new SuperType() without any arguments if it requires arguments... but not really because there's no function signatures in JavaScript. Shouldn't calling new SuperType() without any arguments just lead to the argument being undefined? I guess that could lead to errors though.

More importantly, the guide says that there's other reasons why it's wrong to use SubType.prototype = new SuperType();. What are they?

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • you want to know how subtype.prototype works ?? – Avinash Babu Jul 29 '14 at 04:00
  • @AvinashBabu I guess you could say that. More specifically, I want to know why they say not to assign `new SuperType()` to `SubType.prototype`. – Adam Zerner Jul 29 '14 at 04:08
  • 1
    Yes, you can always call a function with no arguments, and the parameters will be undefined, but what if the constructor relies on those arguments to work properly? Or what if there's some side effect in the constructor that should only run when you're creating an actual instance that you're going to use? For those reasons, you should use `Object.create(SuperType.prototype)` instead. – cookie monster Jul 29 '14 at 05:00
  • SuperType may have instance specific members that should be instance specific to Subtype but now end up in SubType's prototype (prototype is shared among instances). And SuperType may need constructor arguments that are not available when you declare SubType, this and other things are explained here: http://stackoverflow.com/a/16063711/1641941 – HMR Jul 29 '14 at 07:06

3 Answers3

1

Most of the times your constructors wont be as simple as assigning the passed values. sometimes constructors does carry out some manipulation on the passed data to bring the new object to the ideal state.Defaulting to undefined would be really bad in such cases.

i would say its a bad practice because this leaves the object in a invalid state to begin with.when your super type is in invalid state all your sub types are going to be invalid as well.

It is always a good practice to use Object.Create whenever you want to establish a relationship between objects

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
  • And you're adding Parent instance specific members on Child.prototype. Maybe you'll shadow them later by doing `Parent.apply(this,arguments)` in Child but you now have them both in Child instance and Child.prototype. If you don't do `Parent.apply(this,arguments)` a lot of unexpected things may happen. – HMR Jul 29 '14 at 07:18
0

See the working here

function name(h,b) { this.h = h; this.b = b; }

function shape(x,y) { this.x = x; this.y = y; }

name.prototype = new shape(1,2);

name.prototype.x // prints 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
  • Now try that again with this.x of shape being an array and creating 2 names. Then try name1.x.push(22) and log out name2.x You should not create an instance of Parent to set as the prototype of Child even if you don't need constructor arguments. In your example calling `shape.call(this,1,2)` in name would be far better as x and y of shape are instance specific and are now instance specific to name. If you'd like to understand how prototype works maybe the following answer can help: http://stackoverflow.com/a/16063711/1641941 – HMR Jul 29 '14 at 07:10
0

prototype is a property of Object - The base object, every JS object is inherit from Object. Object.prototype used to contain all methods and properties for inherit. But Inherited in JS is not really Inherited same as other languages, in JS we should understand that is Inherited = Shared. In a tree of Inherited, their (object) share properties and method in a box is .prototype.

Then you shouldn't use SubType.prototype = new SuperType(). This statement will be overrides base .prototype of Object(), create new box, which has another allocated memory, not share with other objects. This problem makes heavy performance, illegal to inherit(share) in JS.

Davuz
  • 5,040
  • 13
  • 41
  • 61
  • No, that's not right. There's no performance issue there. This is how the inheritance model works in JS. – cookie monster Jul 29 '14 at 04:59
  • Maybe the following can help you understand how inheritance in JavaScript can work by lengthening the prototype chain. http://stackoverflow.com/a/16063711/1641941 Replacing a function instance prototype (SubType is a function instance and has a prototype) is not a problem and needed to lengthen the prototype. You may want to repair `SubType.prototype.constructor` for completeness. – HMR Jul 29 '14 at 07:16