# INDEX
1. Values & Data type
2. Operators
3. Control flow
4. Scope & Hoisting
5. Object & Array
6. This
7. Prototype & Inheritance
8. Function
9. Callback function
10. Closuer
11. Class
12. Others
arguments
객체와 this
객체를 암묵적으로 전달 받기 때문에, 모든 함수 내부에서는 (화살표함수 제외) arguments
와 this
를 사용할 수 있음this
키워드는 항상 함수 내부에서 사용됨let foo = function () {console.dir(this);};
1) "일반 함수 호출" (`Regular function call`)
foo(); // window // window.foo();
2) "메소드 호출" ('Dot Notation (Object Method Call)')
let obj = {foo : foo};
obj.foo(); // object // {foo: ƒ}
3) "생성자 함수 호출" ('"new" Keyword')
let instance = new foo(); // instance // foo {}
4) "call / apply / bind 호출" (`call / apply / bind`)
let jazz = { name: 'delilah' };
foo.call(jazz); // Object // name: "delilah"
foo.apply(jazz); // Object // name: "delilah"
foo.bind(jazz)(); // Object // name: "delilah"
Non Strict Mode : Global Object
var age = 30; //글로벌 객체 // 5. 이 경우에는 this가 window
var person = {
age: 20,
printAge: function () {
bar(); // 3. 해당 함수의 실행문 찾기 // 4. 실행문의 형태에 따라 this의 값 결정
}
};
function bar () { // 2. this가 포함된 함수 찾기
console.log(this.age); //30 // 1. 먼저 this찾기
}
person.printAge();
Strict Mode : undefined
'use strict';
var name = 'delilah';
function foo () {
console.log(this.name); //can not read property 'name' of undefined at foo
}
foo();
var age = 100;
var delilah = {
age : 35,
foo : function foo () {
console.log(this.age); //console.log(delilah.age) 와 같은 의미 //35
}
};
delilah.foo(); //함수 실행문 here! => 실행문 앞에 .(dot)이 위치하며, 그 부분은 객체 delilah 되므로 this는 delilah
--------------------------------------------------------------------------------------------
var age = 100;
var delilah = {
age : 35,
foo : function bar () {
console.log(this.age); // 아래 호출방식에 따라 값이 달라짐
}
};
var willy = {
age : 31,
foo : delilah.foo
};
var foo = delilah.foo; // 이거는 함수
delilah.foo(); //35
willy.foo(); //31
foo(); //100 (window)
--------------------------------------------------------------------------------------------
// 어렵지만 차근차근... ㅎㅎㅎ
var age = 100;
function verifyAge () { // 2.함수 verifyAge의 내부함수이다
return this.age > 21; // 1.this를 찾는다 //8. 20>21의 return value = false
}
var delilah = {
age : 20, // 7.delilah(.dot)age는 20
verifyAge : verifyAge
};
var sevenEleven = {
sellBeer : (customer) => { // 5.this는 매개변수 customer
if (!(customer.verifyAge())) { // 3.verifyAge의 실행문을 찾는다 // 4.customer(매개변수)가 this객체가 된다
return "No beer"; // 9.verifyAge가 false => 의 반대는 => true => 따라서, 출력1 ("No beer")
} else {
return "Beer";
}
}
}
sevenEleven.sellBeer(delilah); // 6.변수sevenEleven의 프로퍼티 sellBeer에서 매개변수 delilah // 10.따라서, return "Beer"
--------------------------------------------------------------------------------------------
function makePerson (name, age) { // 2. 이 makePerson의 실행결과는 {}객체 => 이것이 delilah가 된다
return {
name,
age,
verifyAge: () => { // 4. 여기! // 6. BUT! 여기는 현재 '화살표함수' 따라서, this가 없음
return this.age > 21; // 4. 여기! // 6. this를 찾아 올라가면 함수 makePerson을 찾을 수 있다
} // 9. 따라서, 이 this는 global obj // 10. 결과적으로 undefined>21 는 false
};
}
const delilah = makePerson("delilah", 30); // 1. delilah라는 변수에 담기는 것은? 함수 makePerson의 실행결과 // 7. makePerson의 실행 // 8. 어떻게 실행? 일반함수
if (delilah.verifyAge()) { // 3. delilah.verifyAge 가 가르키는 것은? // 5. 4번의 함수를 여기서 실행!
alert("Yes, Beer!");
} else {
alert("No, Beer!"); // 11. 따라서, false값을 출력! ("No, Beer!")
}
--------------------------------------------------------------------------------------------
인스턴스.메소드()
class Counter {
constructor() {
this.value = 0; // 생성자 호출을 할 경우, this는 new 키워드로 생성한 Counter의 인스턴스입니다
}
increase() {
this.value++
}
decrease() {
this.value--
}
getValue() {
return this.value
}
}
let counter1 = new Counter() // 생성자 호출
counter1.increase()
counter1.getValue() // 1
-----------------------------------------------------------------------
function foo () {
// this = {}; =>라고 생각하면 쉬움
console.log(this) // foo {}
}
new foo(); // foo () = {}
-----------------------------------------------------------------------
function foo () {
// this = {}; =>라고 생각하면 쉬움
this.age = 30;
this.logName = function () {console.log("name")}; // 처럼 만들어줄 수도 있음
console.log(this)
}
new foo(); // foo () = {}
function foo() {} // foo함수 생성
foo(); // foo함수 실행
foo.call(); // foo함수에 call이라는 메소드를 이용해 호출
foo.call(person); // foo함수를. 실행하려고 하는데. this를 person값으로 해!
// ex)
function logAge () {
console.log(this.age); // 20
}
const person = {
age: 20
};
logAge.call(person); // call메소드가 실행된 함수logAge를 실행하는데, 인자를 person으로 지정
위의 예제에서 logAge.call(person) 구문의 2가지 기능!!
(1) logAge 함수의 this를, 첫 번째 인자로 받은 person으로 설정한다.
(2) logAge 함수를 실행한다. (함수 내부 구문들이 실행된다.)
------------------------------------------------------------------
// ex)
let status = "선글라스";
setTimeout(()=> {
const status = "하트";
const data = {
status : "아보카도",
getStatus : function () {
return this.status;
}
};
console.log(data.getStatus());
//"아보카도" //data.status가 된다
console.log(data.getStatus.call(this));
//"선글라스"
//getStatus함수의 this를 인자this로 설정한 후 실행.
//but, 해당함수는 화살표인자라 undefined가 되어, 밖으로 나가 global obj로!
},1000);
spread operator
의 도입으로 굳이 apply를 이용할 필요가 없어짐Math.max(...[5,4,1,6,2]) // 6
// 간단한 call과 apply의 쓰임의 차이점
function add(x,y) {
return this+x+y;
}
add.call(1,2,3)// 6
----------------------------------------
function add(x,y) {
return this+x+y;
}
add.call(1,[2,3])// 6
function.bind (this값, 인자1, 인자2, ...)
function foo () {
console.log("hello");
}
const bar = foo.bind(); // .bind 메소드는 새로운 "함수"를 반환 O && 실행 X
foo.bind()(); // 위와 동일값 / 조금 이상... 따라서 보통 변수를 따로 설정해준다
bar(); // 이제 bar에는 함수가 담겼기 때문에, bar()로 실행해주어야 한다
bar; // function foo () {console.log("hello")} 를 반환
// 복제를 한 후, 복제된 함수를 실행해야, 기존의 foo 함수를 실행 할 수 있다.
------------------------------------------------------------------------------------------------
function foo (a,b,c) {
console.log(this.age); // 35 // this에 delilah // console.log(delilah.age);
console.log(a+b+c); // 6
}
const delilah = {
age : 35
};
const bar = foo.bind(delilah,1); // bar = function foo 이며, this에 delilah이 들어가고, 인자로 1
bar(2,3);
------------------------------------------------------------------------------------------------
function foo (a,b,c,d,e,f) {
console.log(this.age);
console.log(a+b+C+d+e+f);
}
const delilah = {
age : 35
}; // 1->a, 2->b, 3->c.
const bar = foo.bind(delilah,1,2,3); // 변수bar에는 함수foo가 담기며, this는 delilah, 인자로는 abc에 각각 123.
bar(4,5,6); // 4->d, 5->e, 6->f.
** bind / new / 인스턴스 / prototype