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 @ 2019-08-15 18:03  xgqfrms  阅读(134)  评论(7编辑  收藏  举报