Class에서 사용하지마!!!
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
class Test {
hello() {}
helloWorld = () => {}
}
const instance = new Test()
console.log(instance) // Test {helloWorld: ƒ}
console.log(Test.prototype) // {constructor: ƒ, hello: ƒ}
Normal Function은 Test의 Prototype에 선언되어 있다.
Arrow Function은 Test의 Instance에 선언되어 있다.
일단 Javascript의 Class는 설탕 문법인 것을 모두가 알 것이다. Javascript에는 Class가 존재하지 않는다. prototype으로 흉내만 낸 것이다.
Class 문법을 ES5 문법으로 바꾸면 어떻게 변환되는지 보자.
var Test = /** @class */ (function () {
function Test() {
this.helloWorld = function () { };
}
Test.prototype.hello =
Class 내의 Arrow Function는 생성자 안으로 변환된다.
Class 내의 Arrow Function는 생성자 안으로 변환된다고 하였다. prototype에 선언 되는 것이 아니다.
Javascript에서 Class의 상속은 prototype으로 한다. 그러므로 부모를 호출할 수 없으므로 super를 호출할 수 없다.
그리고 Arrow function은 Class내에서 bind하는 것보다 느리다.
---
class A {
test() {}
test2 =() => {}
}
var a = new A();
var b = new A();
var c = new A();
var d = new A();
arrow function인 test2 method는 다른 Shape(Hidden Class)를 할당하고 있다.
그림에서 보는거 와 같이 Nomal function은 같은 Hidden Class를 지정하고 있다.
인스턴스 생성시 마다 메모리를 차지 한다.
Inlince Caching을 할 수가 없다.