01let和const
生活随笔
收集整理的這篇文章主要介紹了
01let和const
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.1 let
作用
聲明局部變量
特性
- 不存在變量提升
- 不能重復聲明
- 不作為window屬性
- 塊級作用域
- 暫時性死區(聲明前不能使用)
1.2 const
作用
生成常量
特性
與let一致
globalThis 對象
- Es5模塊中的this是window
- Es6模塊中的this是undefined
- 函數中的this,如果函數不是作為對象的方法執行,而是單純作為函數執行,this會指向頂層對象,但是嚴格模式下this會返回undefined
- 不管是嚴格模式,還是普通模式,new Function(‘return this’)(),總是會返回全局對象。但是,如果瀏覽器用了 CSP(Content Security Policy,內容安全策略),那么eval、new Function這些方法都可能無法使用。
獲取頂層對象的方法
// 方法一 (typeof window !== 'undefined'? window: (typeof process === 'object' &&typeof require === 'function' &&typeof global === 'object')? global: this);// 方法二 var getGlobal = function () {if (typeof self !== 'undefined') { return self; }if (typeof window !== 'undefined') { return window; }if (typeof global !== 'undefined') { return global; }throw new Error('unable to locate global object'); };總結
以上是生活随笔為你收集整理的01let和const的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云存储精华问答 | 如何选择混合云提供商
- 下一篇: 边缘计算精华问答 | 边缘计算需要Iaa