let + arrow function
class Test {
constructor() {
}
hello() : string {
return "Hello";
}
}
declare let g : object;
if (g instanceof Test) {
() => {
g.hello(); // error
};
}
g
is a Test
when the function is defined, but g
may not be Test
when the function runs.
const + arrow function
class Test {
constructor() {
}
hello() : string {
return "Hello";
}
}
declare const g : object;
if (g instanceof Test) {
() => {
g.hello(); // ok
};
}
g
is immutable, g
is always a Test
after the function is defined.
const + regular function
class Test {
constructor() {
}
hello() : string {
return "Hello";
}
}
declare const g : object;
if (g instanceof Test) {
function f() {
g.hello(); // error
};
}
Within underlying implementation, regular function f
is defined by ES5 var
, instead of ES6 let
. variable hoisting hoists declaration of f
, when g
may not be a Test
.