码迷,mamicode.com
首页 > 其他好文 > 详细

Egret类class和module写法区别

时间:2016-11-16 01:51:05      阅读:774      评论:0      收藏:0      [点我收藏+]

标签:err   str   gis   mod   bug   names   concat   super   erro   

普通类

 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写法区别

标签:err   str   gis   mod   bug   names   concat   super   erro   

原文地址:http://www.cnblogs.com/gamedaybyday/p/6067781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!