为什么下面的示例中的context不能作为window.context访问
它适用于get_context函数,但不适用于context变量,为什么?
请注意它在Deno TypeScript设置中。
declare global {
let context: number
function get_context(): number
}
console.log(get_context()) // works
console.log(window.get_context()) // works
window.get_context = () => 1 // works
console.log(context) // works
console.log(window.context) // Error
window.context = 2 // Error
export {}发布于 2021-08-08 12:17:08
由于context不是块作用域,所以需要使用var而不是let来声明它。您也可以使用const,如果您希望它是只准备好的。
使用
declare var声明变量。如果变量是只读的,则可以使用declare const。如果变量是块作用域的,也可以使用declarelet。
https://stackoverflow.com/questions/68697629
复制相似问题