Javascript class related examples


Part of the code refer to http://hi.baidu.com/ligq/blog/item/103115df9cdc390563279800.html

        MyClass = function () {
            var param = 2; // private
            this.m = 3; // public
            MyClass.staticCount = 5; // static

            this.publicMethod = function () {
                alert(param); // "this" is function publicMethod, invoke class variable.
            };

            var privateMethod = function ()   // Only visible inside Restaurant()
            {
                myPrivateVar = "I can set this here!";
            };

            MyClass.staticMethod = function () {
                MyClass.staticCount++;
            };
        }
        MyClass.prototype.instanceMethod = function () { alert(this.m); }; // can invoke public var.

        //--------------------------------------------------
            var t = new MyClass();
            t.instanceMethod();
            alert(MyClass.staticCount);
            MyClass.staticMethod();
            alert(MyClass.staticCount);

posted @ 2012-08-15 15:47  webglcn  阅读(144)  评论(0编辑  收藏  举报