Last active
November 20, 2018 10:43
-
-
Save danoctavian/faf3668d4defd91483e9203cf204370c to your computer and use it in GitHub Desktop.
javascript function declaration VS function expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function f(x) { | |
console.log(`x this ${this}`) | |
return y(x) + 1 | |
} | |
// calling this here works, y is hoisted | |
console.log(f.bind(123)(3)) | |
function y(y) { | |
console.log(`y args ${arguments[0]}`) | |
console.log(`y this ${this}`) | |
return y * 2 | |
} | |
const g = (x) => { | |
console.log(`g this ${this}`) | |
return z(x) + 1 | |
} | |
// calling this here will crash because lack of z | |
// console.log(g.bind(123)(3)) | |
const z = (x) => { | |
console.log(`z args ${arguments[0]}`) | |
console.log(`z this ${this}`) | |
return 2 * x | |
} | |
console.log(g.bind(123)(3)) | |
// REDECLARATION | |
// the below is legal, doesn't throw | |
// function f() { console.log('REDECLARED') } | |
// the below won't work | |
// const f = (x) => x + 10000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍