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

3
  • 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. Commented Jun 19, 2017 at 20:07
  • @CarlMarkham I got this just now. Commented Jun 19, 2017 at 20:09
  • @BAE Yeh, this and atom in this context would be the same thing, just code readability really. Commented Jun 19, 2017 at 20:10

1 Answer 1

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

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

5 Comments

@MysteryDownvoter why the downvote?
this refers to the global object. You can see this by editing your snippet to use this.value + value the result will be the same.
The addValue scope is not a JS object. Did you mean the call context?
@Bergi possibly, terminology isn't my strong point.
regardless, this does not refer to the method and using this.value is essentially the same as atom.value. Seems like personal preference on their part

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.