public
Created

Minimal Object Pattern

  • Download Gist
prim.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11
exports.constructor = function(){}
 
exports.constructor.prototype = exports
 
exports.create = function(){
return this.constructor.apply(Object.create(this), arguments)
}
 
exports.property = 'foo'
 
exports.method = function(){return this.property}
prim.usage0.js
JavaScript
1 2 3 4 5
// singleton
 
var prim = require('prim')
 
console.assert(prim.method() == 'foo')
prim.usage1.js
JavaScript
1 2 3 4 5 6 7
// OOP 'Class' (MooTools, Prototype.js, etc...)
 
var Prim = require('prim').constructor
 
var myPrim = new Prim
 
console.assert(myPrim.method() == 'foo')
prim.usage2.js
JavaScript
1 2 3 4 5 6 7
// ES5
 
var prim = require('prim')
 
var myPrim = Object.create(prim)
 
console.assert(myPrim.method() == 'foo')
prim.usage3.js
JavaScript
1 2 3 4 5
var prim = require('prim')
 
var myPrim = prim.create()
 
console.assert(myPrim.method() == 'foo')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.