typescript let 声明变量的坑
ts 文件编译后会把 let 编译成 var
test.ts
function fun() {
var fa = '函数体内的';
{
let fb = '函数体内块级作用域的';
console.log(fb);
}
console.log(fa);
console.log(fb);
}
编译后的test.js
function fun() {
var fa = "函数体内的";
{
var fb = "函数体内块级作用域的";
console.log(fb);
}
console.log(fa);
console.log(fb);
}
执行后
函数体内块级作用域的
函数体内的
函数体内块级作用域的
期望的test.js
function fun() {
var fa = "函数体内的";
{
let fb = "函数体内块级作用域的";
console.log(fb);
}
console.log(fa);
console.log(fb);
}
执行后
ReferenceError: fb is not defined
at fun (D:\vsspace\testTS\helloWorld.js:23:14)
at Object.<anonymous> (D:\vsspace\testTS\helloWorld.js:25:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
也就是 js 的 let 块级作用域的声明失效了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。