Class
-
[Javascript] Class (2) ์ธ์คํด์ค, ์ถ๊ฐ ๊ธฐ๋ฅ, ์์์ฌ๋ถ์์ฌ๋ถ์/Javascript 2022. 4. 4. 23:59
#๋ชจ๋์๋ฐ์คํฌ๋ฆฝํธ_๋ฅ๋ค์ด๋ธ ์คํฐ๋๋ฅผ ์งํํ๋ฉด์ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค. ํด๋์ค์ ์ธ์คํด์ค ์์ฑ ๊ณผ์ class Person { // ์์ฑ์ constructor(name) { // 1. ์๋ฌต์ ์ผ๋ก ์ธ์คํด์ค๊ฐ ์์ฑ๋๊ณ this์ ๋ฐ์ธ๋ฉ๋๋ค. console.log(this); // Person {} console.log(Object.getPrototypeOf(this) === Person.prototype); // true // 2. this์ ๋ฐ์ธ๋ฉ๋์ด ์๋ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํ๋ค. this.name = name; // 3. ์์ฑ๋ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ๋ this๊ฐ ์๋ฌต์ ์ผ๋ก ๋ฐํ๋๋ค. } } 1. ์ธ์คํด์ค ์์ฑ๊ณผ this ๋ฐ์ธ๋ฉ 1) new ์ฐ์ฐ์์ ํจ๊ป ํด๋์ค๋ฅผ ํธ์ถํ๋ฉด ํด๋์ค ์์ constructor์ ์ฝ๋๊ฐ..
-
[Javascript]Class (1)์ ์์ ๋ฉ์๋์ฌ๋ถ์์ฌ๋ถ์/Javascript 2022. 3. 29. 21:52
#๋ชจ๋์๋ฐ์คํฌ๋ฆฝํธ_๋ฅ๋ค์ด๋ธ ์คํฐ๋๋ฅผ ์งํํ๋ฉด์ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค. ํด๋์ค๋ ํ๋กํ ํ์ ์ ๋ฌธ๋ฒ์ ์คํ์ธ๊ฐ? ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ ํ๋กํ ํ์ ๊ธฐ๋ฐ ๊ฐ์ฒด์งํฅ ์ธ์ด๋ ํด๋์ค๊ฐ ํ์ ์๋ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ค. ES5์์๋ ํด๋์ค ์์ด๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑ์ ํจ์์ ํ๋กํ ํ์ ์ ํตํด ๊ฐ์ฒด์งํฅ ์ธ์ด์ ์์์ ๊ตฌํํ ์ ์๋ค. // ES5 ์์ฑ์ ํจ์ var Person = (function () { // ์์ฑ์ ํจ์ function Person(name) { this.name = name; } // ํ๋กํ ํ์ ๋ฉ์๋ Person.prototype.sayHi = function () { console.log('Hi! My name is ' + this.name); }; // ์์ฑ์ ํจ์ ๋ฐํ return Perso..