Skip to content

Instantly share code, notes, and snippets.

@hkusu
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save hkusu/42e21a9a3f7e16f70bc6 to your computer and use it in GitHub Desktop.

Select an option

Save hkusu/42e21a9a3f7e16f70bc6 to your computer and use it in GitHub Desktop.
(function() {
'use strict';
// インスタンスエリア -------------------------------------------------------
var Foo = function () {
// Private な インスタンス変数
var privateMemberVar = 99;
// Private な インスタンスメソッド(Private なインスタンス変数にアクセス可)
var privateMemberMethod = function() {
// ...
};
// Public な インスタンス変数
this.publicMemberVar = 99;
// Public な インスタンスメソッド①(Private/Public なインスタンス変数/メソッドにアクセス可)
this.publicMemberMethod1 = function() {
// ...
};
};
// Public な インスタンスメソッド②(Public なインスタンス変数/メソッドにアクセス可)
Foo.prototype.publicMemberMethod2 = function() {
// ...
};
// クラスエリア(基本的にインスタンス側から制限なくアクセスできる)-----------------
// Private な クラス変数
var privateClassVar = 99;
// Private な クラスメソッド
var privateClassMethod = function() {
// ...
};
// Public な クラス変数
Foo.publicClassVar = 99;
// Public な クラスメソッド
Foo.publicClassMethod = function() {
// ...
};
// エクスポート ------------------------------------------------------------
// ブラウザで利用する場合
window['Foo'] = Foo;
// Node.js や Browserify で利用する場合
//module['exports'] = Foo;
})();
<!DOCTYPE html>
<html>
<head lang="ja">
<meta charset="UTF-8">
</head>
<body>
<script src="Foo.js"></script>
<script>
// 例)インスタンス変数の利用
var foo = new Foo();
console.log(foo.publicMemberVar);
// 例)クラス変数の利用
console.log(Foo.publicClassVar);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment