Skip to content

Instantly share code, notes, and snippets.

@shinout
Created April 19, 2011 13:38
Show Gist options
  • Save shinout/927741 to your computer and use it in GitHub Desktop.
Save shinout/927741 to your computer and use it in GitHub Desktop.
this.hoge; // <- getter ? setter? set: function(val) {this._piyo = val} // is this piyo affected to original object?
/* see if just "obj.hoge" is setter or getter */
var obj = {};
Object.defineProperty(obj, 'hoge', {
get: function(){console.log("get")},
set: function(){console.log("set")}
});
obj.hoge;
/* see if this._hoge is readable / writable */
var fuga = {};
Object.defineProperty(obj, 'piyo', {
get: function(){return this._piyo},
set: function(val){this._piyo = val} // this._piyo is hidden!!!
});
console.log(fuga.piyo);
console.log(fuga._piyo);
console.log(fuga);
fuga.piyo = "shinout";
console.log(fuga.piyo);
console.log(fuga._piyo);
console.log(fuga);
fuga._piyo = "fu-ga";
console.log(fuga.piyo);
console.log(fuga._piyo);
console.log(fuga);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment