설모의 기록

Classes Super 본문

언어/TypeScript

Classes Super

HA_Kwon 2017. 12. 28. 18:57

super

Note that if you call super on a child class it is redirected to the prototype as shown below:
만약 자식 클래스에서 super 를 호출했다면 프로토타입으로 리디렉션된다.
class Base {
    log() { console.log('hello world'); }
}

class Child extends Base {
    log() { super.log() };
}
generates:
var Base = (function () {
    function Base() {
    }
    Base.prototype.log = function () { console.log('hello world'); };
    return Base;
})();
var Child = (function (_super) {
    __extends(Child, _super);
    function Child() {
        _super.apply(this, arguments);
    }
    Child.prototype.log = function () { _super.prototype.log.call(this); }; // new 를 하지 않았기 때문에 프로토타입 함수로 call 함
    return Child;
})(Base);
Notice _super.prototype.log.call(this).
This means that you cannot use super on member properties. Instead you should just use this.
이것은 super 를 멤버 프로퍼티에 super 를 사용할 수 없다는 것을 의미합니다. 대신에 this 만을 사용하세요.
class Base {
    log = () => { console.log('hello world'); }
}

class Child extends Base {
    logWorld() { this.log() };
}
Notice since there is only one this shared between the Base and the Child class you need to use different names (here log and logWorld).
Base 클래스와 Child 클래스는 this만 공유되기 때문에 log와 다른 이름인 logWorld 를 사용해야한다.
Also Note that TypeScript will warn you if you try to misuse super:
또한 타입스크립트는 super 를 사용하지 않았다면 경고할 것이다.
module quz {
    class Base {
        log = () => { console.log('hello world'); }
    }

    class Child extends Base {
        // ERROR : only `public` and `protected` methods of base class are accessible via `super`
        logWorld() { super.log() };
    }

}


'언어 > TypeScript' 카테고리의 다른 글

Symbol  (0) 2018.01.03
Promise  (0) 2018.01.02
Classes Emit  (0) 2017.12.28
TypeScript Class  (0) 2017.12.28
TypeScript Index Signature  (0) 2017.12.28
Comments