ํด๋์ค๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ํน์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ํด ๋ณ์์ ๋ฉ์๋๋ฅผ ์ ์ํ๋ ์ผ์ข ์ ํ๋ก, ๊ฐ์ฒด๋ฅผ ์ ์ํ๊ธฐ ์ํ ์ํ(๋ฉค๋ฒ ๋ณ์)์ ๋ฉ์๋(ํจ์)๋ก ๊ตฌ์ฑ๋๋ค.
class Person{
name = 'name' // property : class์ ์ ์ํ ํจ์
call = () =>{...} // ํด๋์ค์ ์ ์ํ ๋ณ์
}
const myPerson = new Person();
myPerson.call();
consol.log(myPerson.name)
new Person()
์ผ๋ก ํธ์ถํ๋ฉด ๋ด๋ถ์ ์ ์๋ ๋ฉ์๋๊ฐ ๋ค์ด์๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋จ
class Test {
constructor(value) {
this.value = value
}
testFunc (){
console.log(this.value)
}
}
const test = new Test("๋ด๊ฐ ๋ณ๊ฒฝํ ๊ฐ");
test.testFunc();
constructor
๊ฐ ์๋ ์คํnew Test('๋ด๊ฐ ๋ณ๊ฒฝํ ๊ฐ')
์ this.value์ ํ ๋น๋๋ค.๐ super
๋ ๋ถ๋ชจ ์ค๋ธ์ ํธ์ ํจ์๋ฅผ ํธ์ถ ์ํฌ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
class Test {
constructor() {
this.test= "test"
}
testFunc (){
console.log(this.test)
}
}
class Test2 extends Test{
constructor() {
super();
this.name = "name"
this.test = "test2"
}
testFunc2 (){
console.log(this.test)
}
}
const test2 = new Test2();
test2.testFunc();
test2.testFunc2();
์์์ ์ ์ธํ ๊ฐ์ ๊ฐ์ง๊ณ ์ฌ ๋ super
๋ฅผ ์ฌ์ฉํด์ ๊ฐ์ ๊ฐ์ ธ ์จ ํ
Test
์์ ์ ์ธ๋ this.test์ ๊ฐ์ Test2์์ ๋ฐ๊ฟ์ค๋ค.
๐ this
๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , class
์ property
๋ฐ๋ก ํ ๋นํ๊ธฐ
//๊ธฐ์กด ES6 ์์ฑ์ ํจ์๋ฅผ ํธ์ถํด์ผํจ
//property
constructor(){
this.testProperty = 'value '
}
//method
testMethod(){...}
//ES7 ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ์ง ์์๋ ๋จ
//property
testproperty = 'value'
// methode
testMethod = () => {...}
class Test {
test= "test"
testFunc = () => {
console.log(this.test)
}
}
class Test2 extends Test{
name = "name"
test = "test2"
testFunc2 = () => {
console.log(this.test)
}
}
const test2 = new Test2();
test2.testFunc();
test2.testFunc2();