๐Ÿ”ฐas variable

๐Ÿšง ๆ–ฝๅทฅไธญ

JS โŸฉ concepts โŸฉ expression โŸฉ function expression โŸฉ as variable

// โญ๏ธ the variable to which the "function expression" is assigned:
// -----------------------------------------------------------------------------
//   โ€ข will have a `name` property
//   โ€ข the name won't change if it's assigned to a different variable.
//   โ€ข (implicit name) if function is anonymous, it will be the "variable name".
//   โ€ข (explicit name) if function is named, it will be the "function name".

// โญ๏ธ (anonymous) function expression
//        โ•ญโ”€โ”€โ”€โ”€ โญ๏ธ โ”€โ”€โ”€โ”€โ•ฎ
const f = function() { }; 
f.name                   // "f" (implicit name = variable name)

const f2 = f;
f2.name;                 // "f" (the `name` won't changeโ—)

// โญ๏ธ arrow function
//        โ•ญโ”€ โญ๏ธ โ”€โ•ฎ
const h = () => {};
h.name;                  // "h" (implicit name also applies to arrow functions)

// โญ๏ธ (named) function expression
//        โ•ญโ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
const g = function game() { };
g.name;                  // "game" (explicit name = function name)

typeof g;       // 'function'
typeof game;    // undefined (`game` is only scoped within the function bodyโ—)

Last updated