Skip to content

Instantly share code, notes, and snippets.

@nkmry
Last active August 29, 2015 13:56
Show Gist options
  • Save nkmry/83fbaf7ad344409d84b5 to your computer and use it in GitHub Desktop.
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
(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);
@nkmry
Copy link
Author

nkmry commented Feb 26, 2014

https://gist.github.com/uupaa/6138353
のやり方だと、プライベート インスタンス 変数 (メソッド) が扱えないので、改良してみた。
コンストラクターを closure にすることで、そこで宣言した変数 (メソッド) がプライベートかつインスタンス依存になる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment