window
console.log(this);
console.log(window);
console.log(this === window);
global
console.log(this);
console.log(global);
console.log(this === global);
var a = 1;
console.log(a); // ------ 1
console.log(window.a); // 1
console.log(this.a); // - 1
var ๊ฐ ์๋ let, const๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ 1 ์ด ์๋ undefined ๊ฐ์ด ๋์ค๋๋ฐ, ์ด๊ฑด ํธ์ด์คํ ์ด ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ๊ฒ์ผ๋ก ๋ณด์
var a = 1;
window.b = 2;
console.log(a, window.a, this.a); // 1 1 1
console.log(b, window.b, this.b); // 2 2 2
window.a = 3;
b = 4;
console.log(a, window.a, this.a); // 3 3 3
console.log(b, window.b, this.b); // 4 4 4
var a = 1;
delete window.a; // ---------------- false
console.log(a, window.a, this.a); // 1 1 1
var b = 2;
delete b; // ----------------------- false
console.log(b, window.b, this.b); // 2 2 2
window.c = 3;
delete window.c; // ---------------- true
console.log(c, window.c, this.c); // ReferenceError: c is not defined
window.d = 4;
delete d; // ----------------------- true
console.log(d, window.d, this.d); // ReferenceError: d is not defined
var func = function (x) {
console.log(this, x);
};
func(1); // ---- Window { ... } 1
var obj = {
method: func
};
obj.method(2); // {method: f} 2
var obj = {
method: function(x) { console.log(this, x); }
};
obj.method(1); // -- {method: f} 1
obj['method'](2); // {method: f} 2
var obj = {
methodA: function() { console.log(this); },
inner: {
methodB: function() { console.log(this); }
}
};
obj.methodA(); // {methodB: f} ( === obj )
obj['methodA'](); // {methodB: f} ( === obj )
obj.inner.methodB(); // {methodB: f} ( === obj.inner )
obj.inner['methodB'](); // {methodB: f} ( === obj.inner )
obj['inner'].methodB(); // {methodB: f} ( === obj.inner )
obj['inner']['methodB'](); // {methodB: f} ( === obj.inner )
var obj1 = {
outer: function () {
console.log(this); // - obj1
var innerFunc = function () {
console.log(this); // Window, obj2
}
innerFunc();
var obj2 = {
innerMethod: innerFunc
};
obj2.innerMethod();
}
};
obj1.outer();
var obj = {
outer: function () {
console.log(this); // - obj
var innerFunc1 = function () {
console.log(this); // Window
}
innerFunc1();
var self = this;
var innerFunc2 = function () {
console.log(self); // obj
}
innerFunc2();
}
};
obj.outer();
var obj = {
outer: function () {
console.log(this); // obj
var innerFunc = () => {
console.log(this); // obj
};
innerFunc();
}
};
obj.outer();
setTimeout(function () { console.log(this); }, 300); // Window
[1, 2, 3, 4, 5].forEach(function (x) {
console.log(this, x); // Window 1, Window 2, Window 3, Window 4, Window 5
});
document.body.innerHTML += '<button id="a">ํด๋ฆญ</button>';
document.body.querySelector('#a')
.addEventListener('click', function (e) {
console.log(this, e); // button object
});
__proto__
๋ผ๋ ํ๋กํผํฐ๊ฐ ์๋ ๊ฐ์ฒด ์์ฑvar Cat = function (name, age) {
this.bark = '์ผ์น';
this.name = name;
this.age = age;
};
var choco = new Cat('์ด์ฝ', 7);
var nabi = new Cat('๋๋น', 5);
console.log(choco, nabi);
Function.prototype.call(thisArg[, arg1[, arg2[, ...]]])
var func = function (a, b, c) {
console.log(this, a, b, c);
};
func(1, 2, 3); // -------------- Window { ... } 1 2 3
func.call({ x: 1 }, 4, 5, 6); // {x: 1} 4 5 6
var obj = {
a: 1,
method: function (x, y) {
console.log(this.a, x, y);
}
};
obj.method(2, 3); // -------------- 1 2 3
obj.method.call({ a: 4 }, 5, 6); // 4 5 6
Function.prototype.apply(thisArg[, argsArray])
var func = function (a, b, c) {
console.log(this, a, b, c);
};
func.apply({x: 1}, [4, 5, 6]); // {x: 1} 4 5 6
var obj = {
a: 1,
method: function (x, y) {
console.log(this.a, x, y);
}
};
obj.method.apply({ a: 4 }, [5, 6]); // 4 5 6
var obj = {
0: 'a',
1: 'b',
2: 'c',
length: 3
};
Array.prototype.push.call(obj, 'd');
console.log(obj); // {0: "a", 1: "b", 2: "c", 3: "d", length: 4}
var arr = Array.prototype.slice.call(obj);
console.log(arr); // ["a", "b", "c", "d"]
function a () {
var argv = Array.prototype.slice.call(arguments);
argv.forEach(function (arg) {
console.log(arg);
});
}
a(1, 2, 3);
document.body.innerHTML = '<div>a</div><div>b</div><div>c</div>';
var nodeList = document.querySelectorAll('div');
var nodeArr = Array.prototype.slice.call(nodeList);
nodeArr.forEach(function (node) {
console.log(node);
});
var str = 'abc def';
// Array.prototype.push.call(str, ', pushed string');
var concatResult = Array.prototype.concat.call(str, 'string');
console.log(concatResult); // [String, "string"]
var everyResult = Array.prototype.every.call(str, function(char) { return char !== ' '; });
console.log(everyResult); // - false
var someResult = Array.prototype.some.call(str, function(char) { return char === ' '; });
console.log(someResult); // -- true
var newArr = Array.prototype.map.call(str, function(char) { return char + '!'; });
console.log(newArr); // ------ ["a!", "b!", "c!", " !", "d!", "e!", "f!"]
var newStr = Array.prototype.reduce.apply(str, [function(string, char, i) { return string + char + i; }, '']);
console.log(newStr); // ------ a0b1c2 3d4e5f6
var obj = {
0: 'a',
1: 'b',
2: 'c',
length: 3
}
var arr = Array.from(obj);
console.log(arr); // ["a", "b", "c"]
function Person(name, gender) {
this.name = name;
this.gender = gender;
}
function Student(name, gender, school) {
Person.call(this, name, gender);
this.school = school;
}
function Employee(name, gender, company) {
Person.apply(this, [name, gender]);
this.company = company;
}
var yn = new Student('์๋', 'female', 'ํ๊ตญ๋');
console.log(yn);
var sw = new Employee('์ ์ฑ', 'male', '์ค์ด๋ง์ผ');
console.log(sw);
var numbers = [10, 20, 3, 16, 45];
var max = min = numbers[0];
numbers.forEach(function(number) {
if (number > max) {
max = number;
}
if (number < min) {
min = number;
}
});
console.log(max, min); // 45 3
var numbers = [10, 20, 3, 16, 45];
var max = Math.max.apply(null, numbers);
var min = Math.min.apply(null, numbers);
console.log(max, min); // 45 3
const numbers = [10, 20, 3, 16, 45];
const max = Math.max(...numbers);
const min = Math.min(...numbers);
console.log(max, min); // 45 3
Function.prototype.bind(thisArg[, arg1[,arg2[, ...]]])
var func = function (a, b, c, d) {
console.log(this, a, b, c, d);
}
func(1, 2, 3, 4); // --- Window { ... } 1 2 3 4
var bindFunc1 = func.bind({ x: 1 });
bindFunc1(5, 6, 7, 8); // {x: 1} 5 6 7 8
var bindFunc2 = func.bind({ x: 1 }, 4, 5);
bindFunc2(6, 7); // ----- {x: 1} 4 5 6 7
bindFunc2(8, 9); // ----- {x: 1} 4 5 8 9
var func = function (a, b, c, d) {
console.log(this, a, b, c, d);
}
var bindFunc = func.bind({ x: 1 }, 4, 5);
console.log(func.name); // --- func
console.log(bindFunc.name); // bound func
var objWithCall = {
outer: function () {
console.log(this); // - objWithCall
var innerFunc = function () {
console.log(this); // objWithCall
};
innerFunc.call(this);
}
};
objWithCall.outer();
var objWithBind = {
outer: function () {
console.log(this); // - objWithBind
var innerFunc = function () {
console.log(this); // objWithBind
}.bind(this);
innerFunc();
}
};
objWithBind.outer();
var obj = {
logThis: function () {
console.log(this);
},
logThisLater1: function () {
setTimeout(this.logThis, 500);
},
logThisLater2: function () {
setTimeout(this.logThis.bind(this), 1000);
}
};
obj.logThisLater1(); // Window { ... }
obj.logThisLater2(); // obj
var obj = {
outer: function () {
console.log(this); // - obj
var innerFunc = () => {
console.log(this); // obj
};
innerFunc();
}
};
obj.outer();
var report = {
sum: 0,
count: 0,
add: function () {
var args = Array.prototype.slice.call(arguments);
args.forEach(function (entry) {
this.sum += entry;
++this.count;
}, this);
},
average: function () {
return this.sum / this.count;
}
};
report.add(60, 85, 95);
console.log(report.sum, report.count, report.average()); // 240 3 80
์๋ ํ์ธ์ ์ ๋ฆฌ ์ ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๊ทธ๋ฐ๋ฐ ๊ธ ๋ด์ฉ ์ค์ ์๋ชป๋ ๋ถ๋ถ์ด ์๋ ๊ฒ ๊ฐ์ ๋๊ธ ๋จ๊น๋๋ค.
'์ ์ญ ๊ณต๊ฐ์์๋ง ๋ฐ์ํ๋ ํน์ดํ ์ฑ์ง' ๋ถ๋ถ์์
'var ๊ฐ ์๋ let, const๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ 1 ์ด ์๋ undefined ๊ฐ์ด ๋์ค๋๋ฐ, ์ด๊ฑด ํธ์ด์คํ ์ด ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ๊ฒ์ผ๋ก ๋ณด์' ์ด๋ผ๊ณ ํ์ จ๋๋ฐ ์ด๊ฑด ํธ์ด์คํ ๋ฌธ์ ๊ฐ ์๋๋ผ, let๊ณผ const ํค์๋๋ฅผ ์ฌ์ฉํ์ ๋๋ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์ ์ญ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ก ํ ๋น์ ํ์ง ์๊ธฐ ๋๋ฌธ์ด๋ผ๊ณ ์๊ณ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ let๊ณผ const๋ฅผ ์ฌ์ฉํ์ ๋๋ ํธ์ด์คํ ์ ๋๋ ๊ฑธ๋ก ์๊ณ ์์ต๋๋ค.
๋จ์ง var์ ๋ค๋ฅด๊ฒ ๋ณ์ ์์ฑ ์ ์ ์ธ๊ณผ ์ด๊ธฐํ ๋จ๊ณ๊ฐ ๋ถ๋ฆฌ๋์ด ์๊ธฐ ๋๋ฌธ์ console๋ก ํ์ธํ์ ๋ var์ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๊ฒ ๋์จ๋ค๊ณ ํฉ๋๋ค. ์๋ ์ฐธ๊ณ ๋ธ๋ก๊ทธ ๋จ๊ฒจ๋๋ ค์~!
https://curryyou.tistory.com/192 (#5. ์ ์ญ ๊ฐ์ฒด ํ๋กํผํฐ ์ฌ๋ถ)
https://medium.com/korbit-engineering/let%EA%B3%BC-const%EB%8A%94-%ED%98%B8%EC%9D%B4%EC%8A%A4%ED%8C%85-%EB%90%A0%EA%B9%8C-72fcf2fac365 (let, const์ ํธ์ด์คํ ์ฌ๋ถ)