Skip to content

Instantly share code, notes, and snippets.

@widged
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save widged/a000c723065d05659a3d to your computer and use it in GitHub Desktop.

Select an option

Save widged/a000c723065d05659a3d to your computer and use it in GitHub Desktop.

Revisions

  1. widged revised this gist Jan 4, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Person.js
    Original 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() {
  2. widged created this gist Jan 4, 2015.
    111 changes: 111 additions & 0 deletions Person.js
    Original 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;

    }());
    4 changes: 4 additions & 0 deletions usage.js
    Original 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())