let & var & initialized bug
let & var & initialized bug
what's wrong with this?
https://github.com/lydiahallie/javascript-questions#9-whats-the-output
- ReferenceError: greetign is not defined
- {}
solution
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
-
function
-
var
https://www.sitepoint.com/5-typical-javascript-interview-exercises/
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11359610.html
未经授权禁止转载,违者必究!