Created
March 15, 2013 10:20
-
-
Save ChenReuven/5168826 to your computer and use it in GitHub Desktop.
JS: Structuring JavaScript Code - Example
This file contains hidden or 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 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