javascript - creating multiple objects with browserify -
i trying use design pattern below:
human.js
function human(name){ this.name = name this.sayname = function(){ console.log(this.name); } } var = new human("bob"); var b = new human("ted");
however haven't used browserify , don't know how in browserify.
what notice when require human.js , try create new object, appears replacing old object.
how use browserify design pattern?
rest of code along lines of:
module.exports = { myhuman:human }
and in file 1:
var human = require('human.js') var ted = new human('ted') ted.sayname();
and in file 2:
var human = require('human.js') var bob = new human('bob') bob.sayname();
with commonjs (browserify), export when call require (however, careful assuming export singleton, it's not case experience). in case, want export human class directly. in each file need instantiate human, require class , instantiate there.
human.js
function human(name) { this.name = name; this.sayname = function() { console.log(this.name); }; } module.exports = human;
file 1
var human = require('./human.js'); var ted = new human('ted'); ted.sayname();
file 2
var human = require('./human.js'); var bob = new human('bob'); bob.sayname();
Comments
Post a Comment