Last active
August 29, 2015 14:12
-
-
Save widged/a000c723065d05659a3d to your computer and use it in GitHub Desktop.
Revisions
-
widged revised this gist
Jan 4, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ // Person Module var Person = (function ( ) { 'use strict'; // Any static variable can be declared here. var Class = function Person() { -
widged created this gist
Jan 4, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,111 @@ // Person Module var Person = (function ( ) { // Any static variable can be declared here. var Class = function Person() { if(!this instanceof Person) { return new Person(); } var instance = this; // Private variables and functions that only // ..other private or public functions may access // ..and cannot be accessed outside this Module var age = 0, maxAge = 30, maxWeight = 80, isAlive = true, weight = 20, name = 'Un-named'; function growOld() { if ( age++ >= maxAge ) { die(); } } function gainWeight() { weight++; if ( weight >= maxWeight ) { die(); } } function loseWeight() { weight--; if ( weight <= 0 ) { die(); } } function die() { isAlive = false; } // All the properties and methods attached to `instance` // will be public and will be accessible in the global scope. // Public Accessors instance.name = function(_) { if(!arguments.length) { return name; } name = _; return instance; } instance.age = function(_) { if(!arguments.length) { return age; } if(_ > maxAge && age < 0) { throw 'illegal age value' } age = _; return instance; } instance.weight = function(_) { if(!arguments.length) { return weight; } if(_ > maxWeight && weight < 0) { throw 'illegal weight value' } weight = _; return instance; } // Public Methods instance.speak = function () { if ( !isAlive ) { alert('Dead man can\'t speak.'); } else { alert('Speaking...'); growOld(); } return instance; }, instance.walk = function () { if ( !isAlive ) { alert('Dead man can\'t walk.'); } else { alert('Walking...'); growOld(); loseWeight(); } return instance; }, instance.eat = function () { if ( !isAlive ) { alert('Dead man can\'t eat'); } else { alert('Eating...'); gainWeight(); } return instance; } instance.toString = function() { return '' + Class.name + ' ' + JSON.stringify({name: name, age: age, weight: weight}); } return instance; } return Class; }()); 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ var bill = new Person().name('Bill').age(40).weight(30); console.log(bill.eat().weight()) console.log(bill.walk().age()) console.log(bill.toString())