Created
August 3, 2019 15:20
-
-
Save ysm-dev/9b417f55eb7fee6e0ac7552c16d30c7d to your computer and use it in GitHub Desktop.
Markdium-๐ป ํ๋ก ํธ์๋ ๋ฉด์ ์ง๋ฌธ - JS #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Person = function(firstName) { | |
| this.firstName = firstName; | |
| this.sayName1 = function() { console.log(this.firstName); }; | |
| this.sayName2 = () => { console.log(this.firstName); }; | |
| }; | |
| const john = new Person('John'); | |
| const dave = new Person('Dave'); | |
| john.sayName1(); // John | |
| john.sayName2(); // John | |
| // ์ผ๋ฐ ํจ์์ 'this'๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ง๋ง, ํ์ดํ ํจ์๋ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. | |
| john.sayName1.call(dave); // Dave (because "this" is now the dave object) | |
| john.sayName2.call(dave); // John | |
| john.sayName1.apply(dave); // Dave (because 'this' is now the dave object) | |
| john.sayName2.apply(dave); // John | |
| john.sayName1.bind(dave)(); // Dave (because 'this' is now the dave object) | |
| john.sayName2.bind(dave)(); // John | |
| var sayNameFromWindow1 = john.sayName1; | |
| sayNameFromWindow1(); // undefined (because 'this' is now the window object) | |
| var sayNameFromWindow2 = john.sayName2; | |
| sayNameFromWindow2(); // John |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment