admin管理员组

文章数量:1312895

class A extends HTMLElement {
  constructor() {
    super()
    return new Proxy(this, {})
  }
}

window.customElements.define('a-element', A)
<a-element></a-element>

class A extends HTMLElement {
  constructor() {
    super()
    return new Proxy(this, {})
  }
}

window.customElements.define('a-element', A)
<a-element></a-element>

How can i Proxy custom element?

When i try it:

Uncaught InvalidStateError: custom element constructors must call super() first and must not return a different object.

Share Improve this question edited Jan 9, 2019 at 17:16 Yairopro 10.4k7 gold badges47 silver badges54 bronze badges asked Apr 8, 2017 at 20:38 Артур ЛаврищевАртур Лаврищев 2604 silver badges9 bronze badges 1
  • I have custom elements which prise two input fields: a primary and a secondary one. I thought I could proxy the return value like you did because I wanted to forward all HTMLInputElement prototype property lookups to the primary input, save for a select few, without having to write get prop(){ return this.#primary.prop; } set prop(x){ this.#primary.prop = x; } 50 times. Solved it without Proxies by doing this in my module: export const Custom = (class Custom extends HTMLElement{ #primary; static init(){ /* Add getters on `this.prototype` accessing `this.#primary`. */ } }).init();. – Sebastian Simon Commented Oct 21, 2022 at 1:11
Add a ment  | 

1 Answer 1

Reset to default 10

You can either Proxify a class or an instance of a Custom Element.

Given the following Custom Element definition:

class A extends HTMLElement {
  constructor() {
    super()
  }
  connectedCallback() {
    this.innerHTML = 'Hello'    
  }
}
customElements.define( 'a-element', A )

Proxy for a Custom Element instance

Create a Proxy from an instance reference (here: ae):

<a-element id="ae"></a-element>
<script>
  var b1 = new Proxy( ae, {
    get ( target, value ) { return target[value] }       
  } )
  console.log( b1.innerHTML ) // => "Hello"
</script>

Proxy for a Custom Element class

Define the construct trap to catch the new operator:

<script>
  var B = new Proxy( A, {
    construct() { return new A }
  } )
  var b2 = new B
  document.body.appendChild( b2 )
  console.log( b2.innerHTML ) // => "Hello"
</script>

Get a Custom Element instance Proxy from a class Proxy

If you want to instanciate a Custom Element and get one Proxy object directly, you can bine both solutions above.

The following example shows how to get a Proxy for element <a-element> that will log in the console each property access. The construct() trap defined in the class Proxy returns itself a Proxy for the instanciated Custom Element.

class A extends HTMLElement {
  constructor() {
    super()
  }
  connectedCallback() {
    this.innerHTML = 'Hello'    
  }
}
customElements.define( 'a-element', A )		

var P = new Proxy( A, {
  construct () { 
    return new Proxy ( new A, {
      get ( target, value ) { 
        if ( value == 'element' ) 
          return target
        console.info( `proxy: property ${value} for <${target.localName}> is "${target[value]}"` )
        return target[value]
      }       
    } )
  }
} )
var p = new P
document.body.appendChild( p.element )
console.log( p.innerHTML )

本文标签: javascriptHow to Proxy Custom Element (Web Component)Stack Overflow