Skip to content

Instantly share code, notes, and snippets.

@oriSomething
Created August 13, 2017 12:58
Show Gist options
  • Save oriSomething/b30d9d4135a6673087a31c3404cf7b3c to your computer and use it in GitHub Desktop.
Save oriSomething/b30d9d4135a6673087a31c3404cf7b3c to your computer and use it in GitHub Desktop.

Revisions

  1. oriSomething created this gist Aug 13, 2017.
    23 changes: 23 additions & 0 deletions file.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    var obj = {
    _fooValue: 1,

    _fooGetterShouldReturnThis: false,

    get foo() {
    var value = this._fooGetterShouldReturnThis ? this : this._fooValue;
    this._fooGetterShouldReturnThis = false;
    return value;
    },

    set foo(value) {
    this._fooGetterShouldReturnThis = true;
    this._fooValue = value + 1;
    }
    }


    var foo = (obj.foo = 10);

    console.log(foo); // Output `10` instead of `Object { ··· }` or even `11`
    console.log(obj.foo); // Output `Object { ··· }` which is `obj`
    console.log(obj.foo); // Output `11` which is `obj._fooValue`