Javascript: Objects: getters and setters?

Created on 10 Jan 2016  路  2Comments  路  Source: airbnb/javascript

It seems that getters and setters are absent from the guide. I assume they would be mentioned in close proximity to Objects. Do you ever use them? Do you have any reasons not to?

I have mainly used them for syntax concision, but MDN also mentions this potential benefit of using get:

Getters give you a way to define a property of an object, but they do not calculate the property's value until it is accessed. A getter defers the cost of calculating the value until the value is needed, and if it is never needed, you never pay the cost.

I typically use it in place of methods that require no arguments and would otherwise be prefixed as such:

class Example {
  // bad
  getFoo () {
    return this._foo;
  }

  // good 
  get foo () {
    return this._foo;
  }
}

question

Most helpful comment

I have a strong dislike of getters and setters - they make code harder to read, understand, and maintain, by obscuring a function call and pretending it's a property access - with the sole dubious benefit of "you don't have to type parens".

They makes it impossible to know for sure if something has side effects, and make it possible for foo.bar = baz; foo.bar === baz to not be true. In addition, they cause huge performance slowdowns, and instantly makes your code incompatible with ES3 browsers, which Airbnb still supports.

(It is true that there are things in JS that aren't getters/setters that can make foo.bar = baz; foo.bar === baz be false, and that can cause property retrieval and property assignment to have side effects - but this isn't a justification for encouraging these things in more places)

The guide should, at the very least, mention that they should not be used if support for ES3 browsers is required. Ideally, it would also discourage them for the reasons I mentioned.

All 2 comments

I have a strong dislike of getters and setters - they make code harder to read, understand, and maintain, by obscuring a function call and pretending it's a property access - with the sole dubious benefit of "you don't have to type parens".

They makes it impossible to know for sure if something has side effects, and make it possible for foo.bar = baz; foo.bar === baz to not be true. In addition, they cause huge performance slowdowns, and instantly makes your code incompatible with ES3 browsers, which Airbnb still supports.

(It is true that there are things in JS that aren't getters/setters that can make foo.bar = baz; foo.bar === baz be false, and that can cause property retrieval and property assignment to have side effects - but this isn't a justification for encouraging these things in more places)

The guide should, at the very least, mention that they should not be used if support for ES3 browsers is required. Ideally, it would also discourage them for the reasons I mentioned.

Thanks for sharing those reasons. I wasn't aware of them being significantly detrimental to performance, but I suppose that makes sense. I believe I've only used them with Babel-transformed scripts anyway (not really being executed as getters).

The guide should, at the very least, mention that they should not be used if support for ES3 browsers is required. Ideally, it would also discourage them for the reasons I mentioned.

Yes, those are valid things to consider. I understand the concern with side-effects, and would agree with a principle of not mutating anything within a getter:

class Person {
  get fullName () {
    return `${this.firstName} ${this.lastName}`;
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zurfyx picture zurfyx  路  3Comments

stephenkingsley picture stephenkingsley  路  3Comments

brendanvinson picture brendanvinson  路  4Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

danielfttorres picture danielfttorres  路  3Comments