Tuesday, July 12, 2011

Static Member In JavaScript

I did not realize until recently that you can create static member for your custom classes in JavaScript.

JavaScript: You can do so much with so little.

// Class Person
function Person(lastName, firstName){

    this.LastName   = lastName;
    this.FirstName  = firstName;

    this.getFullName = function(){

        return this.LastName + " " + this.FirstName;
    }
}
// Static member of the class Person
Person.create = function(lastName, firstName){
    return new Person(lastName, firstName);
}

var p1 = new Person("Torres","Frederic");
print(p1.getFullName());

var p2 = Person.create("Torres","Frederic");
print(p2.getFullName());

No comments:

Post a Comment