Skip to content

Instantly share code, notes, and snippets.

@tomsato
Last active January 17, 2016 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomsato/eb7b6919e06d3908acad to your computer and use it in GitHub Desktop.
Save tomsato/eb7b6919e06d3908acad to your computer and use it in GitHub Desktop.
function get_set_class() {
var x = 0;
// ここにプライベートは変数や関数を宣言できる
return {
// 公開メソッド
get:function() {return x;},
set:function(num) {x = num; return this;} // return thisはメソッドチェーン用
}
}
var hoge = get_set_class();
print(hoge.get()); // 0
hoge.set(3);
print(hoge.get()); // 3
print(hoge.set(5).get()); // 5 (メソッドチェーン)
// xには直接アクセスできない
print(x); // Exception: ReferenceError: Can't find variable: x
print(hoge.x); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment