1

Are D1, D2(and by extension, d1, d2) equivalent? What are the notable differences between them, if any?

Also, how should I go about making the prototypes of D1 and D2 unmodifiable via Base.prototype? Such that neither gets baz as shown.

var d1, d2
function Base() {this.foo = 'foo'}
Base.prototype.bar = 'bar'

function D1() { }
D1.prototype = new Base()

function D2() {Base.call(this)}
D2.prototype = Object.create(Base.prototype)

Base.prototype.baz = 'baz'

d1 = new D1()
d2 = new D2()

console.log(d1.foo, d1.bar, d1.baz)
//-> foo bar baz
console.log(d2.foo, d2.bar, d2.baz)
//-> foo bar baz
Prashanth Chandra
  • 2,672
  • 3
  • 24
  • 42
  • 1
    No, [they're not equivalent](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here), check *where* `.foo` is created. And why would you make them "unmodifiable", isn't that the whole point of inheritance to reflect such changes? – Bergi May 18 '15 at 21:37

0 Answers0