The following defines, creates and finally inserts an instance of my "autonomous custom element" onto the container:
class MyCustomElement extends HTMLElement {
static get elementName() {
return 'my-custom-element';
}
constructor() {
const self = super();
let _myPrivateData = 'privateValue';
// the following does not cause the callback to be invoked at all!
MyCustomElement.prototype.connectedCallback = function() {
console.log('connected!');
};
return self;
}
//// this would've got invoked anyways, but there's no access to the `_myPrivateData`
// connectedCallback() {
// console.log('connected!');
// }
}
let myCustomElementName = MyCustomElement.elementName;
customElements.define(myCustomElementName, MyCustomElement);
let myCustomElement = document.createElement(myCustomElementName);
let container = document.getElementById('container');
container.appendChild(myCustomElement);
<div id="container"></div>
I've defined the connectedCallback
inside of the "constructor" to have access to _myPrivateData
, but then the callback does not get invoked at all! Having the identical code-excerpt immediately after the "class body" does cause the callback to be invoked in expense of not having access to _myPrivateData
.
Why is that? How shall I approach the issue to have access to _myPrivateData
(preserving its "private" nature) and have the callback invocation working?