Last active
August 29, 2015 14:10
-
-
Save wenzowski/f40a894402017c644ee1 to your computer and use it in GitHub Desktop.
module pattern with require
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 construct(ctor) { | |
function P(args) { return ctor.apply(this, args) } | |
P.prototype = ctor.prototype | |
return function create(args) { | |
return new P(args) | |
} | |
} | |
module.exports = ExampleProto | |
function ExampleProto() { | |
if (!(this instanceof ExampleProto)) return ExampleProto.prototype.create(arguments) | |
this.arguments = Array.prototype.slice.call(arguments) | |
} | |
ExampleProto.prototype.create = construct(ExampleProto) | |
ExampleProto.prototype.pub = function () { | |
return 'public' | |
} | |
function priv() { | |
return 'private' | |
} | |
ExampleProto.prototype.callPriv = function () { | |
return priv() | |
} |
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
var ExampleProto = require('./ExampleProto') | |
console.log('can be called with new:', (new ExampleProto) instanceof ExampleProto) | |
console.log('can be called as a function:', ExampleProto() instanceof ExampleProto) |
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
var ExampleProto = (function () { | |
function construct(ctor) { | |
function P(args) { return ctor.apply(this, args) } | |
P.prototype = ctor.prototype | |
return function create(args) { | |
return new P(args) | |
} | |
} | |
function ExampleProto() { | |
if (!(this instanceof ExampleProto)) return ExampleProto.prototype.create(arguments) | |
this.arguments = Array.prototype.slice.call(arguments) | |
} | |
ExampleProto.prototype.create = construct(ExampleProto) | |
ExampleProto.prototype.pub = function () { | |
return 'public' | |
} | |
function priv() { | |
return 'private' | |
} | |
ExampleProto.prototype.callPriv = function () { | |
return priv() | |
} | |
return ExampleProto | |
})() | |
console.log('can be called with new:', (new ExampleProto) instanceof ExampleProto) | |
console.log('can be called as a function:', ExampleProto() instanceof ExampleProto) |
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
class ExampleProto | |
construct = (ctor) -> | |
P = (args) -> | |
ctor.apply @, args | |
P:: = ctor:: | |
create = (args) -> | |
new P(args) | |
create: construct(ExampleProto) | |
constructor: (args...) -> | |
return ExampleProto::create(args) unless @ instanceof ExampleProto | |
@arguments = args | |
priv = -> | |
"private" | |
pub: -> | |
"public" | |
callPriv: -> | |
priv() | |
console.log 'can be called with new:', (new ExampleProto) instanceof ExampleProto | |
console.log 'can be called as a function:', ExampleProto() instanceof ExampleProto |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment