Renzo Calla
Renzo Calla

Reputation: 7706

Reference to object from grand child?

Let say a I have an object:

var person= {
      firstName:"Renzo",
      getFirstName:function(){
        console.log(this);// is the person object
        return this.firstName;
      },
      address:{
        number:function(){
          console.log(this);// is the number function
          //return person.getFirstName(); // it is working
          return this.firstName;// not working           
        }
      }       
   };
   
console.log(person.getFirstName());   
console.log(person.address.number());   

I know that "this" in the getFirstName method will reference to the person object, but "this" in the number method will not reference to the person object will reference to the number function.., I can access the getFirstName method from number function referencing to the person object, that will solve the problem but...

Question: Is there way to reference the person object from the number method? without using the person variable... is there some special keyword like "this", to access the person method??

Upvotes: 4

Views: 94

Answers (4)

kvn
kvn

Reputation: 2300

You can use ES6 classes and arrow functions to be able to achieve this.

class Person {
    constructor() {
        this.firstName = "Renzo";
        this.address = {
            getNumber: () => this.firstName
        }
  }

  getFirstName() {
    return this.firstName;
  }
}

var person = new Person();
console.log(person.address.getNumber());

Here is a working fiddle - https://jsfiddle.net/sgsvenkatesh/qtg1s2f2/1/

If you are looking for a reserved keyword for accessing the grand parent, there is no such thing. You will need to tweak your code to be able to do it.

Upvotes: 1

chazsolo
chazsolo

Reputation: 8484

Another way you could do this is during the call itself:

var person = {
  firstName: 'Renzo',
  getFirstName() {
    return this.firstName;
  },
  address: {
    number: function() {
      return this.firstName;
    }
  }
}

// then call it
person.address.number.call(person); // 'Renzo'

Upvotes: 2

Jonas Wilms
Jonas Wilms

Reputation: 138457

You could construct the address, so that it has access to its parent object:

function Person(n){
 this.firstName=n;
 this.address=new Adress(this);
}
Person.prototype= {
  firstName:"Renzo",
  getFirstName:function(){
    console.log(this);// is the person object
    return this.firstName;
  }
};

function Address(p){
 this.parent=p;
}
Address.prototype={
   number:function(){
      console.log(this);// is the number
      console.log(this.parent);//the parent
      return this.parent.getFirstName(); // it is working      
    }
 };

Usecase:

console.log((new Person("Me")).address.number());

Upvotes: 1

Ashish Yadav
Ashish Yadav

Reputation: 3196

You can use it like this

      var person= {
      firstName:"Renzo",
      getFirstName:function(){
        return this.firstName;
      },
      address:{
        number:function(){
          return person.firstName;
        }
      }       
   };


console.log(person.address.number()); 

Upvotes: 0

Related Questions