简体   繁体   English

为什么我不能在TypeScript类中声明局部变量和函数?

[英]Why can't I declare local variables and functions within a TypeScript class?

In TypeScript, I can't seem to declare a function in a class without the compiler adding it to the prototype. 在TypeScript中,如果没有编译器将其添加到原型中,我似乎无法在类中声明函数。 For example: 例如:

class MyTypeScriptClass {
    // method, is added to prototype
    foo1(): void {
        alert('invoked foo1');
    }

    // private method also added to prototype
    private foo2(): void {
        alert('invoked foo2');
    }

    //// can I have a local function, without making it a private method?
    //function foo3() {
    //    alert('invoked foo3');
    //}
}

The above compiles to this: 以上编译如下:

var MyTypeScriptClass = (function () {
    function MyTypeScriptClass() { }
    MyTypeScriptClass.prototype.foo1 = function () {
        alert('invoked foo1');
    };
    MyTypeScriptClass.prototype.foo2 = function () {
        alert('invoked foo2');
    };
    return MyTypeScriptClass;
})();

What I am looking for is typescript that can compile to the following JavaScript: 我正在寻找的是可以编译为以下JavaScript的typescript:

var fvm = new FlasherViewModel2();
    var MyTypeScriptClass = (function () {
        function MyTypeScriptClass() { }
        MyTypeScriptClass.prototype.foo1 = function () {
            alert('invoked foo1');
        };
        MyTypeScriptClass.prototype.foo2 = function () {
            alert('invoked foo2');
        };
        function foo3() {
            alert('invoked foo3');
        }
        return MyTypeScriptClass;
    })();

Can it be done? 可以吗?

(As a side note, I know that foo3 would not be callable from external code. I would actually invoke foo3 from another method within the class, for example, to pass a function to a jQuery fadeOut.) (作为旁注,我知道foo3不能从外部代码调用。我实际上会从类中的另一个方法调用foo3,例如,将函数传递给jQuery fadeOut。)

As apsillers mentions, private static is probably what you want. 正如apsillers所提到的, private static可能就是你想要的。 While it's not supported in current builds, you will be able to have a private static member in TypeScript at some point in the future (the design team changed its mind on this one based on feedback similar to this). 虽然它在当前版本中不受支持,但您可以在未来的某个时间点在TypeScript中拥有一个private static成员(设计团队根据类似的反馈改变了对此的想法)。

I just discovered another way to have private methods in a typescript class, though the pattern may smell a little funny. 我刚刚发现了另一种在typescript类中使用私有方法的方法,尽管这种模式可能闻起来有点滑稽。 As far as I can tell, you can only do this when you wrap the class in a module . 据我所知道的,当你裹在班上你只能做这个module For example: 例如:

module MyApp {
    // not accessible externally, `this` must be passed in if needed
    function foo3(that: MyTypeScriptClass): void {
        that.foo1();
        alert('invoked foo3');
    }

    // not accessible externally
    function foo4(): void {
        alert('invoked foo4');
    }

    export class MyTypeScriptClass {
        // normal method, is added to prototype
        foo1(): void {
            alert('invoked foo1');
        }

        // private method also added to prototype, is accessible externally
        private foo2(): void {
            alert('invoked foo2');
            foo3(this);
            foo4();
        }
    }
}

The above compiles into: 以上编译成:

var MyApp;
(function (MyApp) {
    function foo3(that) {
        that.foo1();
        alert('invoked foo3');
    }
    function foo4() {
        alert('invoked foo4');
    }
    var MyTypeScriptClass = (function () {
        function MyTypeScriptClass() { }
        MyTypeScriptClass.prototype.foo1 = function () {
            alert('invoked foo1');
        };
        MyTypeScriptClass.prototype.foo2 = function () {
            alert('invoked foo2');
            foo3(this);
            foo4();
        };
        return MyTypeScriptClass;
    })();
    MyApp.MyTypeScriptClass = MyTypeScriptClass;    
})(MyApp || (MyApp = {}));

With the above, external javascript could invoke foo2() on an instance of MyTypeScriptClass , but neither foo3() nor foo4() is accessible externally at runtime. 有了上述内容,外部javascript可以在MyTypeScriptClass的实例上调用foo2() ,但foo3()foo4()都不能在运行时从外部访问。 The biggest caveat is that if your private method needs access to members of the instance, you have to pass this in as a function parameter. 最大的警告是,如果您的私有方法需要访问实例的成员,则必须将this作为函数参数传递。 Essentially, these are private static methods. 基本上,这些是私有静态方法。

var instance = new MyApp.MyTypeScriptClass();

// public, accessible externally
instance.foo1();

// will not compile in a .ts file, but works at runtime from manual js file
instance.foo2();

// runtime exception, foo3 is not defined
foo3(instance);
MyApp.foo3(instance);

// runtime exception, foo4 is not defined
foo4();
MyApp.foo4();

This approach also sort of works with scalar variables, but the variables are also essentially static -- you can't have different values for different class instances. 这种方法也适用于标量变量,但变量本质上也是静态的 - 不同的类实例不能有不同的值。 To do that, as far as I can tell, you still need to declare them within the class. 要做到这一点,据我所知,你仍然需要在类中声明它们。 Marking them private will keep the typescript compiler from allowing external calls to them, but other javascript code can still access them externally. 将它们标记为private将使打字稿编译器不允许外部调用它们,但其他javascript代码仍然可以从外部访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在jQuery模板中声明local / temp变量吗? - Can I declare local/temp variables within a jQuery template? 您可以在TypeScript的本地方法中访问本地静态变量吗? - Can you access local static variables within local method in TypeScript? 为什么我不能在打字稿类中定义接口或类型 - why i can't define interface or type inside typescript class 我可以将函数导入typescript类文件吗? - Can I import functions into a typescript class file? 为什么变量在Javascript中的函数之前声明 - why variables declare before functions in Javascript 为什么我不能使用绑定方法清空 class 中的数组? - Why can't I empty array within class with bound method? 我可以防止意外覆盖TypeScript / JavaScript中的局部变量吗? - Can I prevent accidental overwrite of local variables in TypeScript / JavaScript? 如何在 JavaScript/TypeScript 中动态读取/写入局部变量? - How can I read/write local variables dynamically in JavaScript/TypeScript? 在在Typescript中向其添加函数之前,如何将变量声明为空对象? - How can I declare a variable to be an empty object before I add functions to it in Typescript? 为什么我不能像传递其他变量一样将函数从 Express.js 传递给 EJS? - Why can't I pass functions from Express.js to EJS like I pass other variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM