1

does anyone know why test1 fails to compile?

class Y { public myMethod: any; };
class QQ { public test(name, fun: () => any) { } }

var qq = new QQ();
qq.test("Run test1", () => {

        var outer = 10;

        Y.prototype.myMethod = () => {

          // Error: The name 'outer' does not exist in the current scope
            outer = 11;
        }
});

But the following works:

   qq.test("Run test2", () => {

            var outer = 10;
            var fun = ()=> { outer = 11; };

            Y.prototype.myMethod = fun;
    });

The JavaScript version of the required code would look look like this:

qq.test("Run test1", function () {
    var outer = 10;

    Y.prototype.myMethod = function () {
        outer = 11;
    };
});

The outer function declares a variable "outer" within its closure, which should naturally be visible to the inner function.

0

2 Answers 2

1

Shortened to just the salient points:

This is the JavaScript I think you are expecting.

var Y = (function () {
    function Y() { }
    Y.prototype.myMethod = function () {
    };
    return Y;
})();
var QQ = (function () {
    function QQ() { }
    QQ.prototype.test = function (name, fun) {
        fun();
    };
    return QQ;
})();
var qq = new QQ();
qq.test("Run test1", function () {
    var _this = this;
    _this.outer = 10;
    Y.prototype.myMethod = function () {
        alert(_this.outer);
    };
});
var y = new Y();
y.myMethod();

You need to change your TypeScript to get this output:

class Y { 
    public myMethod() {

    }
}

class QQ {
    public test(name, fun: () => any) { // updated signature
        fun(); // call the function
    }
}

var qq = new QQ();

qq.test("Run test1", () => {
        this.outer = 10; // use this.
        Y.prototype.myMethod = () => {
            alert(this.outer);
        }
});

var y = new Y();
y.myMethod();

And yes, TypeScript believes this.outer to be a problem in the alert statement, but compiles the correct JavaScript anyway. You can raise that as a bug at http://typescript.codeplex.com.

Sign up to request clarification or add additional context in comments.

3 Comments

Sohnee, I don't quite follow. There are two lambda expressions here. The outer lambda defines a variable "outer" and then assigns the inner lambda to some object's prototype. The outer lambda defines a closure, which means that all objects within the outer closure should be visible within the inner closure. This is very standard JavaScript - nothing at all funky about it. I believe this is a bug in the TypeScript compiler.
@NoelAbrahams clarification needed - I have posted a JavaScript example and questioned what difference you expect.
'@Sohnee, I've edited the question to show the expected result. The solution you have proposed does not seem natural to me, a "this" in a lambda seems strange. I'll raise this as a bug, if there is nothing more. Thanks.
0

This was a bug in TypeScript until verion 0.8.2 that has been fixed since then.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.