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;
    }

 

转载于:https://www.cnblogs.com/gamedaybyday/p/6067781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值