Home > Software engineering >  Object being recognized as null or undefined
Object being recognized as null or undefined

Time:09-09

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);

CodePudding user response:

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

Even though functions are Objects in JavaScript the 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 (though it's not common to use proto according to some articles)

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

CodePudding user response:

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);

CodePudding user response:

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);

  • Related