如何进行类实现? [英] How to do class implementation?

查看:111
本文介绍了如何进行类实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeScript编译类如下:

TypeScript compiles a class something like:

var UrlProvider = (function(){
        //tons of logic in here that only needs to be performed once for each UrlProvider instance
        function UrlProvider(baseUrl){
            var baseRequest = {
                get: function(){return baseUrl;},
                update: function(){return baseUrl;},
                delete: function(){return baseUrl;}
            };
            var documents = function(){
                var context = '/documents/';
                return{
                    get: function(){return baseRequest.get() + context;},
                    post: function(){return baseRequest.post() + context;},
                    delete: function(){return baseRequest.delete() + context;}
                }
            };
            var editors = function(){
                var context = '/editors/';
                return{
                    get: function(){ return baseRequest.get() + context; },
                    post: function(){ return baseRequest.post() + context; },
                    delete: function(){ return baseRequest.delete() + context; }
                }
            }
        }
        return UrlProvider;
    })();

将逻辑放在UrlProvider构造函数之外,但在外部IIFE的闭包内有什么好处?我的想法是,如果我们需要一个远程服务或其他一些昂贵的过程来创建UrlProviders,那么它可能会更好地放置在外部闭包中而不是UrlProvider的构造函数中?它是否正确?将逻辑放在构造函数外部,但在IIFE中有什么好处?

Is there any benefit to putting logic outside of the UrlProvider constructor, but inside the closure of the outer IIFE? My thinking was that perhaps if we needed a remote service or some other expensive process to create UrlProviders that could possibly be better placed in the outer closure vs. the constructor of the UrlProvider? Is this correct? IS there any benefit to putting logic in outside the constructor, but inside the IIFE?

推荐答案


IS将逻辑放在构造函数外部有什么好处,但在IIFE内部

IS there any benefit to putting logic in outside the constructor, but inside the IIFE

是的。继承需要IIFE来捕获基类。如下所示

Yes. The IIFE is needed for inheritance to capture the base class. This is shown below

class Foo {
    log() { }
}

class Bar extends Foo {
    log() {
        super.log(); // IIFE needed for `super` to work
    }
}

看在生成的javascript(我删除了extends函数)。

Look at the generated javascript (I've removed the extends function).

var Foo = (function () {
    function Foo() {
    }
    Foo.prototype.log = function () {
    };
    return Foo;
})();

var Bar = (function (_super) {
    __extends(Bar, _super);
    function Bar() {
        _super.apply(this, arguments);
    }
    Bar.prototype.log = function () {
        _super.prototype.log.call(this); // IIFE needed for `super` to work
    };
    return Bar;
})(Foo);

_super 由IIFE捕获。原因是函数是唯一在JavaScript中创建变量范围的东西,这就是为什么我们在codegen中创建一个IIFE以便以一个漂亮的本地名称捕获基类( _super )。这是传统的JavaScript,并非特定于TypeScript。

_super is captured by the IIFE. Reason is that functions are the only thing that create a variable scope in JavaScript and that is why we create an IIFE in the codegen to capture the base class in a nice local name (_super). This is conventional JavaScript, not specific to TypeScript.

这篇关于如何进行类实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆