美文网首页
typescript的class转换成ES5的代码样子

typescript的class转换成ES5的代码样子

作者: 漫漫江雪 | 来源:发表于2019-10-21 11:53 被阅读0次
    class Handler {
        static greet: string = 'hello';
        constructor(public info: string) {}
        setInfo = (inf: string) => { // 箭头表达式
            this.info = inf
        }
        normalFun = function () {
            console.log('我是一个普通函数')
        }
        myMethod() {
            console.log('只有fun()这种形式的方法才会被生成到 prototype上')
        }
    }
    

    转换后的代码:

    "use strict";
    var Handler = /** @class */ (function () {
        function Handler(info) {
            var _this = this;
            this.info = info;
            this.setInfo = function (inf) {
                _this.info = inf;
            };
            this.normalFun = function () {
                console.log('我是一个普通函数');
            };
        }
        Handler.prototype.myMethod = function () {
            console.log('只有fun()这种形式的方法才会被生成到 prototype上');
        };
        Handler.greet = 'hello';
        return Handler;
    }());
    

    static成员被挂在了构造函数上, 用户箭头函数或者 = function赋值的形式,会变成每个实例都有的属性(这会增加额外的开销), methodName()的形式才会被挂在prototype上

    相关文章

      网友评论

          本文标题:typescript的class转换成ES5的代码样子

          本文链接:https://www.haomeiwen.com/subject/nqqtvctx.html