xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

let & var & initialized bug

let & var & initialized bug

what's wrong with this?

https://github.com/lydiahallie/javascript-questions#9-whats-the-output

  1. ReferenceError: greetign is not defined

image

image

  1. {}

image

image


solution

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Strict_mode_for_modules

https://github.com/lydiahallie/javascript-questions/issues/33#issuecomment-521590062

let-initialize & ES module & "use strict"; bug

OK

// "use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-08-15
 *
 * @description let-initialize-ok & ES module & "use strict"; bug
 * @augments
 * @example
 * @link
 *
 */

let log = console.log;

const test = () => {
    let greeting;
    greetign = {}; // Typo!
    log(greetign);
};
test();
// ??? greeting, not initialized
// ReferenceError: greetign is not defined


const testComputed = () => {
    var greetign = {}; // Typo!
    let greeting;
    log(greetign);
};
testComputed();
// {}


"use strict"; & bug

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-08-15
 *
 * @description let-initialize & ES module & "use strict"; bug
 * @augments
 * @example
 * @link
 *
 */

let log = console.log;

let greeting;
// greeting, not initialized

// temporal dead zone,
// it is not accessible before the line we declare (initialize) it;
// When we try to access the variables before they are declared, JavaScript throws a ReferenceError.

greetign = {}; // Typo!

log(greetign);
// ReferenceError: greetign is not defined


scope & hoisting

https://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

https://repl.it/@xgqfrms/Function-hoisting-greater-var-hoisting

https://stackoverflow.com/questions/7506844/javascript-function-scoping-and-hoisting

js var hoisting

  1. function

  2. var

https://www.sitepoint.com/5-typical-javascript-interview-exercises/



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @   xgqfrms  阅读(134)  评论(7编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-08-15 钉钉-开放平台 & 微信 share
2016-08-15 面试秘籍:如何在面试前判断一家公司靠不靠谱? All In One
点击右上角即可分享
微信分享提示