1

I am learning airbnb coding style. Why use atom.value instead of this.value in the following codes (section 3.3)? any benefits?

// good
const atom = {
  value: 1,

  addValue(value) {
    return atom.value + value;
  },
};

Update

The following code is an example of its benefits. Any other benefits?

const bias = atom.addValue;
console.log(bias(11))

Thanks

BAE
  • 8,550
  • 22
  • 88
  • 171
  • 2
    Haven't looked at the docs, but they possibly prefer the use of `atom` because `this` is ambiguous depending on the scope. Using `atom` is a clear indication of the scope intended. – Cjmarkham Jun 19 '17 at 20:07
  • @CarlMarkham I got this just now. – BAE Jun 19 '17 at 20:09
  • @BAE Yeh, `this` and `atom` in this context would be the same thing, just code readability really. – Cjmarkham Jun 19 '17 at 20:10

1 Answers1

0

If you use this it will refer to addValue scope:

addValue(value) {
  return this.value + value;
}

Here this will not refer to atom but the addValue() which does not have any value attribute.

const atom = {
  value: 1,

  addValue(value) {
    console.log( atom.value + value);
    return atom.value + value;
  },
};

atom.addValue(20);
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108