사용자 요소/웹 구성 요소 클래스가 등록 된 후에 수정할 수 있습니까? 이것은 프로덕션 코드에서 일반적으로 (또는 지금까지) 수행 할 작업이 아니지만 개발을위한 코드 패칭 도구를 프로토 타이핑하거나 구현할 때 유용 할 것입니다. 여기에 지금까지 내가 실패, 시도했습니다 무엇 : 원래 CustomElementsRegistry에 전달 된 클래스는 아무런 영향을 미치지 않습니다 수정 요약에서사용자 정의 요소 클래스 정의 후 수정
document.body.appendChild(document.createElement('foo-bar')) // empty element
class FooBar extends HTMLElement {
connectedCallback() {
this.textContent = 'foo bar'
}
}
customElements.define('foo-bar', FooBar) // element shows "foo bar"
FooBar.protoype.connectedCallback = function() {
this.textContent = 'foo bar 2'
}
document.body.appendChild(document.createElement('foo-bar')) // element shows "foo bar", not "foo bar 2"
customElements.get('foo-bar').protoype.connectedCallback = function() {
this.textContent = 'foo bar 2'
}
document.body.appendChild(document.createElement('foo-bar')) // element shows "foo bar", not "foo bar 2"
class FooBar2 extends HTMLElement {
connectedCallback() {
this.textContent = 'foo bar 2'
}
}
customElements.define('foo-bar', FooBar2) // Exception thrown, 'foo-bar' already defined
. 레지스트리에있는 프로토 타입에 할당하는 것은 아무 효과가 없습니다. 예외가 발생하면 레지스트리의 요소에 새 클래스를 할당하려고 시도 할 수 없습니다. 이 세 가지 동작은 다른 모든 JavaScript API의 거의 모든 측면에 대한 나의 경험에 역행합니다. 더 좋든 나쁘 든간에 구성 할 수 없도록 설정하지 않는 한 무언가를 변경할 수 없도록하는 것은 매우 드문 경우입니다. 그러나 객체를 구성 할 수 있는지 검사 할 때 런타임에 나타납니다!
정의 된 요소 정의를 수정/보강하는 방법을 알고있는 사람이 있습니까?
'customElements.define'은 제공된 클래스의 다양한 멤버 함수에 대한 참조를 얻고이를 무시하려고 시도하는 것으로 보입니다. – Intervalia