Last active
January 17, 2016 11:04
-
-
Save tomsato/eb7b6919e06d3908acad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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