使用动态扩展 class 表达式创建新元素
Creating new element with a dynamic extends class expression
是否可以传递一个class表达式作为参数?
还没有尝试过 eval
路线..
// CardtsElements.Zone contains a valid class expression
// used to create a valid Zone Custom Element
let extend = (source, name, definitionClassExpression) =>
customElements.define('CARDTS-' + name,
class extends CardtsElements[source] definitionClassExpression);
^^^^SYNTAX ERROR^^^^^^^^^^
// Create a new 'CARDTS-FOUNDATION' element extending 'CARDTS-ZONE'
extend('Zone','Foundation', {
static get observedAttributes() {
return ['suit','draggable','drop'];
}
constructor(){}
});
您可以将 class 表达式 作为 class 工厂 函数传递:
- a(箭头)
function
、
- 带一个参数(
superclass
):你要扩展的class
,
- 这将 return 新派生的
class
。
// CardtsElements.Zone contains a valid class expression
// used to create a valid Zone Custom Element
var CardtsElements = {
'Zone': class extends HTMLElement {
constructor() { super() ; console.log('zone element created') }
connectedCallback(){ console.log('connected Zone')}
zone() { console.log( 'zone' ) }
}
}
let extend = (source, name, classFactory) =>
customElements.define('cardts-' + name, classFactory(CardtsElements[source]))
// Create a new 'CARDTS-FOUNDATION' element extending 'CARDTS-ZONE'
extend('Zone','foundation', superclass =>
class extends superclass {
constructor() { super() ; console.log(this.localName + ' created') }
static get observedAttributes() { return ['suit','draggable','drop'] }
connectedCallback(){
super.connectedCallback();
console.log('connected',this.localName)
}
foundation() { console.log('foundation') }
}
)
CF.zone()
CF.foundation()
<cardts-foundation id=CF>Cardts Foundation</cardts-foundation>
是否可以传递一个class表达式作为参数?
还没有尝试过 eval
路线..
// CardtsElements.Zone contains a valid class expression
// used to create a valid Zone Custom Element
let extend = (source, name, definitionClassExpression) =>
customElements.define('CARDTS-' + name,
class extends CardtsElements[source] definitionClassExpression);
^^^^SYNTAX ERROR^^^^^^^^^^
// Create a new 'CARDTS-FOUNDATION' element extending 'CARDTS-ZONE'
extend('Zone','Foundation', {
static get observedAttributes() {
return ['suit','draggable','drop'];
}
constructor(){}
});
您可以将 class 表达式 作为 class 工厂 函数传递:
- a(箭头)
function
、 - 带一个参数(
superclass
):你要扩展的class
, - 这将 return 新派生的
class
。
// CardtsElements.Zone contains a valid class expression
// used to create a valid Zone Custom Element
var CardtsElements = {
'Zone': class extends HTMLElement {
constructor() { super() ; console.log('zone element created') }
connectedCallback(){ console.log('connected Zone')}
zone() { console.log( 'zone' ) }
}
}
let extend = (source, name, classFactory) =>
customElements.define('cardts-' + name, classFactory(CardtsElements[source]))
// Create a new 'CARDTS-FOUNDATION' element extending 'CARDTS-ZONE'
extend('Zone','foundation', superclass =>
class extends superclass {
constructor() { super() ; console.log(this.localName + ' created') }
static get observedAttributes() { return ['suit','draggable','drop'] }
connectedCallback(){
super.connectedCallback();
console.log('connected',this.localName)
}
foundation() { console.log('foundation') }
}
)
CF.zone()
CF.foundation()
<cardts-foundation id=CF>Cardts Foundation</cardts-foundation>