Wednesday, 22 July 2015

Adding public and private method/property




Here is how you returns a Person object to the caller:

function Person(name)
{
    // increase the head count (static property)
    this.constructor.headCount++;

    // private variable
    var _dob;
    var _name = name;

    //public functions
    this.helo = function () { return 'helo work..' };
    this.toString = function () { return _name };

    // public property
    this.money = 1000;
}

// public functions
Person.prototype.sayMyName = function () { return this.toString() };
Person.prototype.myBalance = function () { return this.money; };

// static property
Person.headCount = 0;

var p1 = new Person('mike');
var p2 = new Person('john');
var count = Person.headCount;    // the output is '2' because you called 'new' twice

For detail explanation, please refers to the following artcile:
http://phrogz.net/JS/classes/OOPinJS.html

No comments:

Post a Comment

We are moving

We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm