Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ChenReuven/5168826 to your computer and use it in GitHub Desktop.
Save ChenReuven/5168826 to your computer and use it in GitHub Desktop.
JS: Structuring JavaScript Code - Example
var myNS = window.myNS || {};
// Prototype Revelaing Pattern
/*myNS.Customer = function ( name, address, email){
'use strict';
this.name = name;
this.address = address;
this.email = email;
};
myNS.Customer.prototype = function(){
'use strict';
var getName = function(){
return this.name;
},
getAddress = function(){
return this.address;
},
getEmail = function(){
return this.email;
};
return {
getName : getName,
getAddress : getAddress,
getEmail : getEmail
};
};*/
// Module Revailng Pattern
/*myNS.Customer = function ( _name, _address, _email){
'use strict';
var name = _name,
address = _address,
email = _email,
getName = function(){
return name;
},
getAddress = function(){
return address;
},
getEmail = function(){
return email;
};
return {
getName : getName,
getAddress : getAddress,
getEmail : getEmail
};
}*/
// IIFE
(function(myNS){
myNS.Customer = function ( _name, _address, _email){
'use strict';
var name = _name,
address = _address,
email = _email,
getName = function(){
return name;
},
getAddress = function(){
return address;
},
getEmail = function(){
return email;
};
return {
getName : getName,
getAddress : getAddress,
getEmail : getEmail
};
}
})(myNS = window.myNS || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment