I'd add one more to this list ... in fact I would make it the most important. I'll preface this with the disclaimer that module patterns can be important if you are writing code to be used by 3rd parties, but there is no need to do it for internal code. note what methods/variables are public and having coding standards. use standard APIs for object creation. Use bundlers/minifiers to produce module code with private members. Most source to source compilers or bundlers like webpack, browserify, rollup, GCC can take your normal code and turn it into an AMD, CJS, IIFE, whatever format.
// _fields are private
class Example {
constructor(name, x, y) {
this.name = name;
this._x = x;
this._y = y;
}
doSomething(a, b) {
return a * x + b * y;
}
_secret() {
// some private method
}
}
And to make it a singleton... just export a new'd version.
2
u/strident-octo-spork Dec 19 '15
I'd add one more to this list ... in fact I would make it the most important. I'll preface this with the disclaimer that module patterns can be important if you are writing code to be used by 3rd parties, but there is no need to do it for internal code. note what methods/variables are public and having coding standards. use standard APIs for object creation. Use bundlers/minifiers to produce module code with private members. Most source to source compilers or bundlers like webpack, browserify, rollup, GCC can take your normal code and turn it into an AMD, CJS, IIFE, whatever format.
And to make it a singleton... just export a
new'd version.