Home > Enterprise >  Difference between: new ExampleConstructor and Object.create(ExampleConstructor)
Difference between: new ExampleConstructor and Object.create(ExampleConstructor)

Time:02-11

Code as attached

function Bird() {
  let weight = 15;
  this.getWeight = () => weight
}

// let birb = new Bird
let birb = Object.create(Bird.prototype);

console.log(birb.getWeight())

When I use new Bird it returns a function fine. However, when I use Object.create(Bird.prototype), I get a TypeError telling me birb.getWeight is not a function.

Why does creation from the prototype interfere with the function?

CodePudding user response:

getWeight doesn't exist on the prototype. It is created on the object itself when the constructor function is run (which isn't happening when you use Object.create instead of calling the function).

  • Related