Appendix A: Exploring Further - hochan222/Everything-in-JavaScript GitHub Wiki

Values vs. References

JS์—์„œ ๊ฐ’ ์ž์ฒด๋ฅผ ํ• ๋‹น/์ „๋‹ฌํ•˜๋ฉด ๊ฐ’์ด ๋ณต์‚ฌ๋œ๋‹ค.

var myName = "Kyle"; 
var yourName = myName;

myName์— ์ €์žฅ๋ผ์žˆ๋Š” "Kyle"์˜ ๋ฌธ์ž์—ด๊ณผ๋Š” ๋ณ„๋„๋กœ ๋ณต์‚ฌ๋œ ์‚ฌ๋ณธ์ด yourName์— ์ €์žฅ๋œ๋‹ค. ๊ทธ ์ด์œ ๋Š” primitive value์ด๊ณ  ํ• ๋‹น/์ „๋‹ฌ ์‹œ์— ํ•ญ์ƒ ๋ณต์‚ฌ๋ณธ์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค.

var myName = "Kyle";
var yourName = myName;
myName = "Frank";

console.log(myName);
// Frank
console.log(yourName);
// Kyle

JS์—์„œ๋Š” ๊ฐ์ฒด ๊ฐ’ (๋ฐฐ์—ด, ๊ฐ์ฒด, ํ•จ์ˆ˜ ๋“ฑ) ๋งŒ ์ฐธ์กฐ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

var myAddress = {
street: "123 JS Blvd", city: "Austin", state: "TX"
};
var yourAddress = myAddress;
// I've got to move to a new house!
myAddress.street = "456 TS Ave"; console.log(yourAddress.street);
// 456 TS Ave

Primitive๋Š” ๊ฐ’์œผ๋กœ ์œ ์ง€๋˜๊ณ  Object๋Š” ์ฐธ์กฐ๋กœ ์œ ์ง€๋œ๋‹ค.

So Many Function Forms

๋‹ค์Œ์€ ์‹๋ณ„์ž๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ต๋ช…ํ•จ์ˆ˜์ธ๋ฐ JS์—์„œ๋Š” ์ต๋ช… ํ•จ์ˆ˜์— ๋Œ€ํ•ด "์ด๋ฆ„ ์ถ”๋ก "์„ ์‹œํ–‰ํ•œ๋‹ค.

var awesomeFunction = function(coolThings) { 
  // ..
  return amazingStuff; 
};

awesomeFunction.name;
// "awesomeFunction"

์ด๋ฆ„ ์ถ”๋ก ์€ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์ด ํ• ๋‹น๋œ ๊ฒฝ์šฐ๊ฐ™์ด ์ œํ•œ๋œ ๊ฒฝ์šฐ์—๋งŒ ๋ฐœ์ƒํ•œ๋‹ค.
๋‹ค์Œ์€ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜ ์„ ์–ธ ์–‘์‹๋“ค์ด๋‹ค.

// generator function declaration
function *two() { .. }

// async function declaration
async function three() { .. }

// async generator function declaration
async function *four() { .. }

// named function export declaration (ES6 modules)
export function five() { .. }

๋‹ค์Œ์€ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜ ํ‘œํ˜„์‹ ํ˜•์‹๋“ค์ด๋‹ค.

// IIFE
(function(){ .. })(); (function namedIIFE(){ .. })();

// asynchronous IIFE
(async function(){ .. })();
(async function namedAIIFE(){ .. })();

// arrow function expressions
var f;
f = () => 42;
f = x => x * 2;
f = (x) => x * 2;
f = (x,y) => x * y;
f = x => ({ x: x * 2 });
f = x => { return x * 2; }; f = async x => {
var y = await doSomethingAsync(x);
return y * 2; };

someOperation( x => x * 2 ); // ..

Coercive Conditional Comparison

var x = "hello";

if (Boolean(x) == true) { 
  // will run
}

// which is the same as:
if (Boolean(x) === true) { 
  // will run
}

Prototypal โ€œClassesโ€

var Classroom = { 
  welcome() {
    console.log("Welcome, students!"); 
  }
};

var mathClass = Object.create(Classroom);

mathClass.welcome();
// Welcome, students!
function Classroom() { 
  // ..
}

Classroom.prototype.welcome = function hello() { 
  console.log("Welcome, students!");
};

var mathClass = new Classroom();

mathClass.welcome();
// Welcome, students!