0

I have created one constructor function named House in JS.

function House(color, floor, location){
    this.color = color;
    this.floor = floor;
    this.location = location;    
}

Using this prototype I have created two more objects named myHouse and myOldHouse.

var myHouse = new House('brick red', '1st floor', 'saltlake');
var myOldHouse = new House('Yellow', '2st floor', 'Koikhali');

Then I have added one new method called address to the myHouse object.

myHouse.address = function(address){
   this.address = address;
   console.log('My address is '+this.address); 
}

Now I am want to add this method to myOldHouse object as well using the call function.

myHouse.address.call(myOldHouse, '2G');

But when I am calling myOldHouse.address(), it is showing error.

Run_Script
  • 2,146
  • 2
  • 10
  • 24
  • 1
    Because in your address function you are setting address to be parameter and then you run the call but address value is not a function any more. – Nenad Vracar Nov 30 '19 at 15:11

2 Answers2

0

I like your way , it is tradicional method. Please use setAddress or something like that.

Mistake : - You can not have method and also variable with same name on same level. Call your setter function setAddress. - Don't attach new method on object (instance of class) do it on class direct. Try this :

function House(color, floor, location) {
   
    this.color = color;
    this.floor = floor;
    this.location = location;    
    this.address = "";
}

Address = function( color, floor, location , address) {
   House.call(this, color, floor, location);
   this.address = address;
   console.log('My address is '+this.address + " color is: ", this.color); 
};

var myHouse = new Address('brick red', '1st floor', 'saltlake', "country1");
var myOldHouse = new Address('Yellow', '2st floor', 'Koikhali', "country2");

// myHouse.setAddress ('new address');

It is not strange or stupid this way of class definition. You can always put you class in single file to make sure everything is clear.

Tradicional class definition will prevent any error on older version of browsers and still work on all modern web navigators.

Nikola Lukic
  • 3,277
  • 5
  • 33
  • 57
  • I tried your solution but getting Uncaught TypeError: Cannot set property 'setAddress' of undefined at script.js:27 –  Nov 30 '19 at 15:12
  • I updated answer. I must say big mistake was attaching methot to the object (instance of class) you need to do it on class. – Nikola Lukic Nov 30 '19 at 15:19
  • I know, the way I have written down the code is not usual. But here I am trying to experiment with call method to borrow method from another object. My aim here is to apply the call(). Not to create a method using prototype. –  Nov 30 '19 at 15:36
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call – Nikola Lukic Nov 30 '19 at 20:48
0

Call when invoked in a method (address in your case) just tells the scope the method should run in. When passing myOldHouse, In the address method the this keyword refers to myOldHouse object. So now myOldHouse.address must print out '2G'. Here in your case, you are accessing a method that is unaccessible for an object here. The root cause is that the method address is not shared between the two objects, namely myHouse and myOldHouse. You've created these objects from House function which means these two will have access to all the properties associated with House .In your example, you've associated address method to myHouse which restricts it's access to the objects created from myHouse function.

For example:

var myHouseChild = new myHouse(...);

Now, myHouseChild object will have access to both the properties of House and myHouse functions.

In order to make address method work for myOldHouse you have to set it in the prototype of House function so that it is shared among it's objects.

Like, House.prototype.address = () => {};

Refer this answer for inner workings of new keyword.

Aswath
  • 96
  • 4