the murmurous sea

JS ES6: Functions hoisting check 본문

#dev/개념정리

JS ES6: Functions hoisting check

lunacer 2020. 6. 3. 11:40

After

//1. var
console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))

var minus = (a,b) => a - b;
//typeof minus: undefined
//Uncaught TypeError: minus is not a function at check.js:4
//(anonymous) @ check.js:4


//2. const
console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))

const minus = (a,b) => a - b;
//Uncaught ReferenceError: Cannot access 'minus' before initialization at check.js:13
//(anonymous) @ check.js:13


//3. let
console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))

let minus = (a,b) => a - b;
//Uncaught ReferenceError: Cannot access 'minus' before initialization at check.js:22
//(anonymous) @ check.js:22


//4. function
console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))

function minus(a,b){ return a - b };
//typeof minus: function
//minus(5,1): 4


//5. arrow function without 
console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))

minus = (a,b) => a - b;
//typeof minus: undefined
//Uncaught ReferenceError: minus is not defined at check.js:41
//(anonymous) @ check.js:41

 

ReferenceError & TypeError ▼

더보기


The 
ReferenceError object represents an error when a non-existent variable is referenced.
The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.

 

1. var

Uncaught TypeError: minus is not a function.

: minus is declared but before defined.

: when there was an attempt to call a value from a function, but the value is not actually a function.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function)

Why it's not actually a function?

'minus' is added under the window but it's 'undefined' yet. I guess it gives this error because it's declared but as undefined, which is one of the primitive data types. Not object(object, array or function)

 

2.3. const, let 

Uncaught ReferenceError: Cannot access 'minus' before initialization

: Temporal dead zone

: when a lexical variable was accessed before it was initialized. This happens within any block statement, when let or const declarations are accessed before they are defined.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init)

 

4. function

: no error

 

5. arrow without

Uncaught ReferenceError: minus is not defined

: when there is a non-existent variable referenced somewhere.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined)

 

[Comparison] Before

//1. var
var minus = (a,b) => a - b;

console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))
//typeof minus: function
//minus(5,1): 4


//2. const
const minus = (a,b) => a - b;

console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))
//typeof minus: function
//minus(5,1): 4


//3. let
let minus = (a,b) => a - b;

console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))
//typeof minus: function
//minus(5,1): 4


//4. function
function minus(a,b){ return a - b };

console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))
//typeof minus: function
//minus(5,1): 4


//5. arrow function without 
minus = (a,b) => a - b;

console.log('typeof minus:', typeof minus)
console.log('minus(5,1):', minus(5,1))
//typeof minus: function
//minus(5,1): 4

'#dev > 개념정리' 카테고리의 다른 글

JS: about Event  (0) 2020.06.03
JS: EventTarget.addEventListener() (empty)  (0) 2020.06.03
JS: Literal (incompleted)  (0) 2020.05.27
Git : 생활코딩 요약  (0) 2020.05.22
Progressive Web App (PWA)  (0) 2020.05.22
Comments