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!