Classes in JavaScript

    
$(document).ready(function () {
        var p = new Person("Mihai");
        p.showSecond();
    });

    function Person(name) {
        this.name = name;

        // this is part of class, it's owned by the object
        this.showSecond = function () { alert(this.name + " " + Person.DefaultAge); }
    }

    // it's like a extension method
    Person.prototype.showName = function () {
        alert(this.name);
    }

    // simulate a static
    Person.DefaultAge = 20;
Advertisements