Created
September 9, 2012 18:51
-
-
Save keriati/3686426 to your computer and use it in GitHub Desktop.
Module pattern ...
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
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<script type="text/javascript" src="jquery.js"></script> | |
<script type="text/javascript"> | |
(function() { | |
/** | |
* The MYNS module | |
* | |
* @module MYNS | |
*/ | |
var MYNS = {}; | |
/** | |
* The Person class in MYNS | |
* | |
* @class Person | |
* @constructor | |
*/ | |
MYNS.Person = (function() { | |
var Constr = function(firstName) { | |
/** | |
* | |
* @property firstName | |
* @type {String} | |
*/ | |
this.firstName = firstName; | |
/** | |
* | |
* @property _lastName | |
* @type {String} | |
* @private | |
*/ | |
this._lastName = 'Smith'; | |
}; | |
/** | |
* The Private function | |
* | |
* @method _privateFunction | |
* @private | |
*/ | |
function _privateFunction() { | |
console.log('Private Function :)'); | |
} | |
/** | |
* Public function in the prototype: return the name. | |
* | |
* @method getFullName | |
* @return {String} | |
*/ | |
function getFullName() { | |
_privateFunction(); | |
return this.firstName + ' ' + this._lastName; | |
} | |
function setLastName(lastName) { | |
this._lastName = lastName; | |
} | |
// Revealing module pattern for the prototype | |
Constr.prototype = { | |
getFullName: getFullName, | |
setLastName: setLastName | |
}; | |
return Constr; | |
})(); | |
// Expose MYNS to the global object | |
window.MYNS = MYNS; | |
})(); | |
(function () { | |
var Adam = new MYNS.Person('Adam'), | |
Eva = new MYNS.Person('Eva'); | |
log(); | |
Eva.setLastName('Jones'); | |
log(); | |
function log() { | |
console.log('Objects: ', Adam, Eva); | |
console.log('Name: ', Adam.getFullName(), Eva.getFullName()); | |
} | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment