0

I'm trying to assign housePrototype as the prototype of House object, and followed up with this error

Object.assign(House.prototype, this.housePrototype); ^ TypeError: Cannot convert undefined or null to object

What am I doing wrong?

const House = {
    name: 'houseObject',
     readLocation: (location)=>{
      console.log(`house is located at ${location}`)
    }
  }

  const housePrototype = {
    readLocation: (location) => {
    console.log(`house prototype is located at ${location}`)
    }
  }

  Object.assign(House.prototype, housePrototype)
  
  House.readLocation('Malabe');
  console.log(House);
Inod Umayanga
  • 114
  • 1
  • 7
  • 1
    `House.prototype` is undefined – Jaromanda X Sep 09 '22 at 03:07
  • But https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes article saying we can set prototype in assign method as below. Can you explain please? – Inod Umayanga Sep 09 '22 at 03:16
  • 1
    isn't it should be `Object.assign(House, housePrototype);`? – TimoDevs Sep 09 '22 at 03:19
  • 1
    you've read that page wrong ... notice the code where `Object.assign(Person.prototype, personPrototype);` is used ... check what `Person` is and compare it to what `House` is – Jaromanda X Sep 09 '22 at 03:19
  • because u dont have property `prototype` in House object – TimoDevs Sep 09 '22 at 03:19
  • @JaromandaX Person is a function and House is an abject but aren't they both supposed to have prototypes? Functions are also objects in JavaScript right? – Inod Umayanga Sep 09 '22 at 03:25
  • @TimoDevs console.log(House.__proto__) prints its prototype to the console doesn't the House.prototype does the same for regular objects? (not function objects) – Inod Umayanga Sep 09 '22 at 03:29
  • 1
    `console.log({}.prototype)` vs `console.log(function() {}.prototype)` should answer your question – Jaromanda X Sep 09 '22 at 03:29
  • @JaromandaX oh wow never realized the difference till now thank you so much – Inod Umayanga Sep 09 '22 at 03:39
  • @InodUmayanga Both objects have prototypes: `Object.getPrototypeOf(House)`, `Object.getPrototypeOf(Person)`. But only the constructor function has a `.prototype` property. – Bergi Sep 09 '22 at 12:37

3 Answers3

0

All credit goes to @JaromandaX. He explained the issue in the comments of the question.

Even though functions are Objects in JavaScript they are NOT identical. To access the prototype of a function function() {}.prototype can be used. But in the question, it is trying to access the prototype of a regular object, not a function object, which is undefined, thus the error.

Object.assign(House.__proto__, housePrototype); 

With this it is possible to assign desired object to the prototype (It is not recommended to assign any variables to proto and it si not at all a common pra)

The solution code would be

const House = {
    name: 'houseObject',
     readLocation: (location)=>{
      console.log(`house is located at ${location}`)
    }
  }

  const housePrototype = {
    readLocation: (location) => {
    console.log(`house prototype is located at ${location}`)
    }
  }

  Object.assign(House.__proto__, housePrototype)
  
  House.readLocation('Malabe');

I found a complete description in following StackOverflow thread __proto__ VS. prototype in JavaScript

Inod Umayanga
  • 114
  • 1
  • 7
  • 1
    No, don't do that. `House.__proto__` is `Object.prototype`, and you should absolutely not assign any properties to that. – Bergi Sep 09 '22 at 12:38
  • @Bergi Thanks and I will not use __proto__ anywhere in a production code. I'll edit the answer not recommending to use it further – Inod Umayanga Sep 10 '22 at 06:40
-2

Hey Bro the problem is House.prototpe because the object house has no object called prototype there are only two name and readLocation so change code to this : from this :

const House = {
    name: 'houseObject',
     readLocation: (location)=>{
      console.log(`house is located at ${location}`)
    }
  }

  const housePrototype = {
    readLocation: (location) => {
    console.log(`house prototype is located at ${location}`)
    }
  }

  Object.assign(House.prototype, housePrototype)
  
  House.readLocation('Malabe');
  console.log(House);

to this :

const House = {
    name: 'houseObject',
     readLocation: (location)=>{
      console.log(`house is located at ${location}`)
    }
  }

  const housePrototype = {
    readLocation: (location) => {
    console.log(`house prototype is located at ${location}`)
    }
  }

  Object.assign(House, housePrototype)
  
  House.readLocation('Malabe');
  console.log(House);
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 14 '22 at 01:17
-2

Usefull docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#try_it

const House = {
  name: 'houseObject',
  readLocation: (location)=>{
    console.log(`house is located at ${location}`)
  }
}

const housePrototype = {
  readLocation: (location) => {
    console.log(`house prototype is located at ${location}`)
  }
}

//Object.assign(target, src);
Object.assign(House, housePrototype);

House.readLocation('Malabe');
console.log(House);
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 13 '22 at 23:32