首页 > 代码库 > Egret类class和module写法区别

Egret类class和module写法区别

普通类

 Test.ts

class Test {
    public name:string = "Test";
    public run(){
         console.log(this.name);
    }
}

var test:Test = new Test();

 编译后的Test.js

var Test = (function () {
    function Test() {
        this.name = "Test";
    }
    var d = __define,c=Test,p=c.prototype;
    p.run = function () {
        console.log(this.name);
    };
    return Test;
}());
egret.registerClass(Test,‘Test‘);

 

Test2.ts

module Test2 {
    export class Test2{
        public name: string = "Test2";
        public run() {
            console.log(name);
         }
    }
}
var test2:Test2 = new Test2.Test2();

 

编译后的Test2.js

var Test2;
(function (Test2_1) {
    var Test2 = (function () {
        function Test2() {
            this.name = "Test2";
        }
        var d = __define,c=Test2,p=c.prototype;
        p.run = function () {
            console.log(name);
        };
        return Test2;
    }());
    Test2_1.Test2 = Test2;
    egret.registerClass(Test2,Test2.Test2);
})(Test2 || (Test2 = {}));

 

 

静态类

Test.ts

class Test {
    public static name:string = "Test";
    public static run(){
         console.log(this.name);
    }
}
Test.run();

 

 编译后的Test.js

var Test = (function () {
    function Test() {
    }
    var d = __define,c=Test,p=c.prototype;
    Test.run = function () {
        console.log(this.name);
    };
    Test.name = "Test";
    return Test;
}());
egret.registerClass(Test,‘Test‘);

 

Test2.ts

module Test2 {
    var name:string = "Test2";
    
    export function run(){
        console.log(name);
    }
}
Test2.run();

 编译后的Test2.js

var Test2;
(function (Test2) {
    var name = "Test2";
    function run() {
        console.log(name);
    }
    Test2.run = run;
})(Test2 || (Test2 = {}));

 

RegisterClass.ts

export function registerClass(classDefinition:any, className:string, interfaceNames?:string[]):void {
        if (DEBUG) {
            if (!classDefinition) {
                $error(1003, "classDefinition");
            }
            if (!classDefinition.prototype) {
                $error(1012, "classDefinition")
            }
            if (className === void 0) {
                $error(1003, "className");
            }
        }
        var prototype:any = classDefinition.prototype;
        prototype.__class__ = className;
        var types = [className];
        if (interfaceNames) {
            types = types.concat(interfaceNames);
        }
        var superTypes = prototype.__types__;
        if (prototype.__types__) {
            var length = superTypes.length;
            for(var i=0;i<length;i++){
                var name = superTypes[i];
                if(types.indexOf(name)==-1){
                    types.push(name);
                }
            }
        }
        prototype.__types__ = types;
    }

 

Egret类class和module写法区别