Last active
August 29, 2015 13:56
-
-
Save nkmry/83fbaf7ad344409d84b5 to your computer and use it in GitHub Desktop.
JavaScript module template that can be used in Browser, WebWorker, Node.js. https://gist.github.com/uupaa/6138353
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(global) { | |
"use strict"; | |
// --- Define ---------------------------------------------- | |
// platform detection | |
var _BROWSER = !!global.self; | |
var _WORKER = !!global.WorkerLocation; | |
var _NODE_JS = !!global.process; | |
// --- Private class variables ----------------------------- | |
var hoge; // This is shared by all instances of this class | |
function Class() { | |
// --- Interface --------------------------------------- | |
this.method = Class_method; // Class#method():void | |
// --- Public instance valuables ----------------------- | |
this.values = 'public'; | |
// --- Implement of interface (public methods) --------- | |
function Class_method(){ | |
} | |
// --- Private instance variables ---------------------- | |
var huga; | |
// --- Private methods --------------------------------- | |
function Class_private_method(){ | |
} | |
} | |
// --- Process to each platform ---------------------------- | |
if (_NODE_JS) { | |
} else if (_WORKER) { | |
} else if (_BROWSER) { | |
} | |
// --- Export ---------------------------------------------- | |
if (_NODE_JS) { | |
module.exports = Class; | |
} | |
global.Class = Class; | |
})(this.self || global); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/uupaa/6138353
のやり方だと、プライベート インスタンス 変数 (メソッド) が扱えないので、改良してみた。
コンストラクターを closure にすることで、そこで宣言した変数 (メソッド) がプライベートかつインスタンス依存になる。