Created
April 8, 2015 14:39
-
-
Save JasonRammoray/12f375ddf94b5c558e9e to your computer and use it in GitHub Desktop.
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
All in one file example: | |
var buisnessModule = (function() { | |
var SomeConstructor = function SomeConstructor() { | |
if( !( this instanceof SomeConstructor ) ) { | |
return new SomeConstructor(arguments); | |
} | |
this.params = Array.prototype.slice.call(arguments); | |
}; | |
(function() { | |
var storage = { | |
fieldA: 'Some string', | |
fieldB: 42, | |
fieldC: { | |
key: 'value' | |
// etc | |
} | |
}; | |
SomeConstructor.prototype.getStorage = function() { | |
return storage; | |
}; | |
})(); | |
return { | |
Entity: SomeConstructor, | |
someOtherField: 'someOtherValue' | |
} | |
})(); | |
(function(module) { | |
// do some stuff using module capabilities | |
})(buisnessModule); | |
Require.js example: | |
Assume that we have a folder which contains index.html (entry point), app.js (which can request module and use it functionality), | |
buisness.js (module definition) and require.js (library itself). | |
Index.html file content: | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<script data-main="app.js" src="require.js"></script> | |
</body> | |
</html> | |
Buisness.js file content: | |
define(function() { | |
var SomeConstructor = function SomeConstructor() { | |
if( !( this instanceof SomeConstructor ) ) { | |
return new SomeConstructor(arguments); | |
} | |
this.params = Array.prototype.slice.call(arguments); | |
}; | |
(function() { | |
var storage = { | |
fieldA: 'Some string', | |
fieldB: 42, | |
fieldC: { | |
key: 'value' | |
// etc | |
} | |
}; | |
SomeConstructor.prototype.getStorage = function() { | |
return storage; | |
}; | |
})(); | |
return { | |
Entity: SomeConstructor, | |
someOtherField: 'someOtherValue' | |
} | |
}); | |
App.js file content: | |
requirejs(['buisness'], function(module) { | |
var entity = module.Entity(4, 8, 15, 16, 23, 42); | |
console.log('Entity params: ', entity.params); | |
console.log('Instances storage: ', entity.getStorage()); | |
// do some other stuff with module | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment