JavaScript에서 상속에 어려움을 겪고 있습니다.JavaScript에서 클래스의 인스턴스를 동적으로 상속받습니다.
class Parent {
constructor({ name, someOtherStuff } = {}) {
this.name = name;
this.someOtherStuff = someOtherStuff;
}
someMethod() {
// ...
}
}
나는 다음 날 할 수 있도록 해주는 장식을 만들고 싶습니다 : : 이제 나는 다음과 같은 클래스가 있다고 가정 해 봅시다
@parent({
name: 'foo',
someOtherStuff: 'bar'
})
class MyClass extends Component {
myMethod() {
// ...
}
}
const instance = new MyClass();
// Those tests must pass
expect(instance.someMethod).toBeFunction();
expect(instance.name).toEqual('foo');
expect(instance.someOtherStuff).toEqual('bar');
expect(instance.myMethod).toBeFunction();
expect(instance instanceof Parent).toBe(true);
expect(instance instanceof MyClass).toBe(true);
등의 장식을 만들 수있는 방법이 있나요? 여러 솔루션을 시도했지만 모든 테스트를 실제로 만족하는 솔루션은 없습니다.
const parent = (...args) => (Target) => {
// Target corresponds to MyClass
const parent = new Parent(...args);
// ...
};
로다 시가 허용됩니다.
"데코레이터"란 데코레이터 같은 파이썬을 의미합니까? – Arnial
@Arnial : https://github.com/wycats/javascript-decorators/blob/master/README.md. –
뜻은 다음과 같습니다. https://github.com/wycats/javascript-decorators – Ch4rAss