3 ms·
Using var to declare a variable later assigned to an anonymous function can help with managing larger JavaScript projects where scoping and data member access i
by reconor 12y ago
Using var to declare a variable later assigned to an anonymous function can help with managing larger JavaScript projects where scoping and data member access is important. Here is a pattern I tend to apply rather frequently, included is an example for a custom instance object, which would be created with 'new', and a Namespace object:
var MyObject;
(function() {
var staticPrivateVariable;
MyObject = function(opts) {
// Do Initialization Stuff
};
MyObject.prototype.publicInstanceFunction = function(opts) {
// Do instance stuff
};
})();
var Namespace;
(function() {
var staticPrivateVariable;
Namespace = function(opts) {
// Do Initialization Stuff
};
Namespace.publicFunction = function(opts) {
};
function privateFunction(opts) {
// Do some private stuff
}
})();