0
function Person()
{
  this.name="Person"
}

Person.prototype.hi=function(){
  alert("Hello! I'm"+this.name)
}

function Iwanttoinherit(){
  ...
}

What is the main difference in OOP inheriting between using

Iwanttoinherit.prototype=new Person()

and

Iwanttoinherit.prototype=Object.create(Person.protoype)
1
  • 1
    By doing new Person() instead of Object.create(...), any change to the Person prototype will also change the Iwanttoinherit prototype, but not the other way around. Any properties created/added in the Person constructor will also be a part of the Iwanttoinherit prototype. If that's your intention, then that's the method you should use. Commented Jan 23, 2015 at 19:17

1 Answer 1

1

The difference between these two is that new Person() causes Iwanttoinherit to inherits the Person properties, while Object.create(Person.prototype) will not.

Object.create(Person.prototype)

This creates an object, with an inheritance chain to: Person.prototype.

However, Person.prototype is just {}, in other words: Object.create({}), which is the same as new Object(), which has no inherited properties

Similarly, new Person() creates a Person object. By assigning Iwanttoinherit.prototype the new Person() value, you're essentially causing the Iwanttoinherit object to inherit the Person properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.