0

How can I access a sibling property from a method in Javascript? It seems like a simple thing to want to do, but I haven't found a way. Specifically, consider the following code.

let f = { 
    a: 3, 
    printMyBrother() { console.log(X) } 
}.printMyBrother

f()

What must I change X to in the above code, in order to programatically log "3" on the console?

8
  • Store your object in another variable (eg o) and reference o.a Commented May 5, 2020 at 1:33
  • That has not answered the question. Commented May 5, 2020 at 1:39
  • 3
    Given the literal code above, what you want is not possible. If f were your object and you called f.printMyBrother(), you could use this.a but since you're referring directly to the printMyBrother function, all context of that original object is lost Commented May 5, 2020 at 1:42
  • 1
    It seems very strange to me that this is not possible. Many languages have a "self" keyword that will reference the containing object. In those languages, we could just substitute X -> self.a. Even in HTML, we can do this, using the parentElement property. I don't understand why we are not able to do this in Javascript. Commented May 5, 2020 at 1:45
  • 2
    You're not going to get a good answer for that question here. You could try asking Crockford but he probably won't answer. In the meantime, these might help... What does it mean that Javascript is a prototype based language?, Difference between classical inheritance and prototype inheritance Commented May 5, 2020 at 1:51

1 Answer 1

-1

You can do it if you change your code a little bit:

This is an example of different ways that this would work. The reason it works this way is due to interesting rules of javascript's this.

let f = {
  a: 3,
  printMyBrother(message) {
    console.log(message, this.a)
  }
}
let printMyBrother = f.printMyBrother;


console.group('function');
console.log('function');
f.printMyBrother('function call')

// The printMyBrother won't work alone without being bound
printMyBrother('unbound function call');

// Bind printMyBrother variable to always refer to f
printMyBrother = printMyBrother.bind(f);
printMyBrother('bound function call')
console.groupEnd();


class ClassF {
  a = 3;
  // This is unbound to the class by default
  // because it's not an arrow function
  // You can bind it in the constructor manually, however
  printMyBrotherUnbound(message) {
    console.log('classF', message, this.a)
  };
  // This is bound to the class by default
  // because it's an arrow function
  // This should work in all major browsers (besides IE)
  printMyBrotherBound = (message) => {
    console.log('classF', message, this.a)
  }
}


let classF = new ClassF();
console.group('Class Method')
console.log('Class Method')
printMyBrother = classF.printMyBrotherUnbound;
classF.printMyBrotherUnbound('in class');
try {
  printMyBrother('unbound');
} catch (error) {
  console.log('unbound class function errors out because this is undefined');
}
console.groupEnd()

console.group('Class Arrow Function');
console.log('Class Arrow Function');
printMyBrother = classF.printMyBrotherBound;
classF.printMyBrotherBound('arrow function');
printMyBrother('unbound arrow function');
console.groupEnd();

Some resources on the this keyword in JavaScript:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

https://www.javascripttutorial.net/javascript-this/

Sign up to request clarification or add additional context in comments.

1 Comment

I am aware of the things you point out. That is why I phrased my question in the manner I did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.