You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Non-class based example of customElement.define() #587
Hello,
I'd like for there to be an available, working examples of autonomous and customized Custom Elements made without use of the class syntax. The Mozilla MDN page for example shows a use of Object.create(HTMLElement.prototype) to create an autonomous custom element on it's Custom Elements page that satisfies this non-class based way of working, however that example doesn't work- it yields Uncaught TypeError: Failed to execute 'define' on 'CustomElementRegistry': The callback provided as parameter 2 is not a function. on customElement.define("my-tag", MyTag).
What is a valid syntax to use now, for creating autonomous and customized Custom Elements? Might we add some examples of such in to the spec?
Reacted by Slava, Boaz Blake, Jelle De Loecker, caishuxiang, Abdul Rauf, Jurgen Gjonçari, Dustin Dowell, Mohamadᶠᵍ, The Jared Wilcurt, Cody Ebberson and 1 more
It's not possible to use custom elements without ES6 classes. That was a design decision necessary to achieve consensus at the January face-to-face meeting.
Closing, since there's nothing actionable here, but happy to continue discussing in the closed thread.
Reacted by Jason Mitchell, dataffinityhealth, José Carlos, Anish Kumar Arya, caishuxiang, Clay Branch, Krishna, Alistair Lynn and Benjamin Atkin
Reacted by Gianluca Guarini, Fatih Altinok, Nemanja Milosavljevic, Joscha Rohmann, Andreas, Joe Pea, Serhii Matrunchyk, Aaron Shafovaloff, Alexander Dorn, Boaz Blake and 55 more
Reacted by dataffinityhealth
Reacted by Aaron Shafovaloff, David Mark Clements, KlarkC, Emil Johansson, Mehdi Raash, matthispaita, Selwyn, Alexander Prinzhorn, Joscha Rohmann, Joe Pea and 26 more
I'd like to see non-class based JS become possible, hopefully in v2. Please re-open this as a request. Classes are syntax, statically constructed, which means we can't create components on the fly with code. This is a serious and frankly scary limitation.
For an example of use, if I wanted to generate components for, say, schema.org, the class based syntax means that I have to manually type out class definitions for ~650 components. Having normal, regular JS objects would have let me use code to generate new components. Please re-open this as an outstanding issue @domenic.
Reacted by Frank Holtmann, David Mark Clements, Giwayume, KlarkC, Emil Johansson, Andres Rios, matthispaita, Fatih Altinok, Abraham Serafino, Mustafa Ekim and 35 more
Sorry, this was a condition of getting consensus, and is now fully built in to the architecture of the feature. It cannot be changed.
Reacted by Alexander Fallenstedt
Reacted by Jelle De Loecker, Roy Prins, Russell Dempsey, Amri Toufali, Alfeu S, Tanner Gaucher, Rein Petersen, PupoSDC, Alexander Praetorius, Arnoldo Paz and 18 more
I hope you're aware that you can generate classes dynamically just as easily as functions, so you certainly would not need to type those out. Classes are just as much syntax as functions are. If you don't know how do do this, please ask on StackOverflow, but not here.
Reacted by Aluan Haddad, Zzzen, Bill Keese, Slava, Matt Clarkson, Mark, Enoh Barbu, Jakub Brzegowski, Jakub Trzebiatowski, Bernard and 5 more
Reacted by Ibrahim Tanyalcin, Jelle De Loecker, sukrainets-blackbird, Anthony White, nasim huq, Tanner Gaucher, Alexander Praetorius, Advaiya Lad, Pavel, Jurgen Gjonçari and 7 more
(Apologies if this issue has already been discussed elsewhere; I had entirely failed to consider it before and I haven’t seen it mentioned…)
Will this cause problems for existing JS codebases that use a WebComponents polyfill with a transpiler like Babel? For example, transpiling this code using Babel’s es2015 preset fails to work because the resulting JS doesn’t use Reflect.construct:
I understand that native custom elements won’t be available in browsers that don’t already support ES-2015 class syntax, but if someone is using Babel + a polyfill for web components, it seems like they’d have a situation where their code works in older browsers (because the polyfill is active), but not in newer ones (because the polyfill just defers to the native implementation). That seems like a pretty big practical problem, but is it one you are concerned about here?
Reacted by Andreas, Jurig Gunung, Maximouse and Kim Helberg
It is true that if you're using Babel and polyfill, then the above code won't work out-of-box but that's true of any polyfill that got written before the standard is finalized.
There are various ways to workaround such issues, and probably the simplest solution is to wrap the thing you pass to customElements.define with a class. e.g.
There are dozens of other ways to cope with this limitations and that's really up to framework and library authors.
On a broader note, I don't think the standards process or API design in standards should be constrained by polyfills written before the general consensus on the API shape has been reached and at least two major browser engines have implemented it. Also, deploying a polyfill on production before the standards have become stable is almost always a bad idea.
It is true that if you're using Babel and polyfill, then the above code won't work out-of-box but that's true of any polyfill that got written before the standard is finalized.
I suppose I was really most focused here on the impact to existing codebases. It’s not as if that hasn’t been a consideration in other web standards, though I do understand that current usage of polyfills for custom elements (and especially v1-esque polyfills) is quite small.
On the other hand, there is a lot of Babel usage out there (the majority of non-trivial JS codebases I’ve worked on as a consultant over the past year have used it), and I hadn’t really expected that I’d need such an awkward and specialized method for creating a custom element with it. It may be further complicated in trying to find solutions that allow someone to inherit from a custom element provided as a third-party module, where the provider of the component may have solved the issue in their own way. As you noted, there are many ways to work around it.
Also, deploying a polyfill on production before the standards have become stable is almost always a bad idea.
I agree! I’ve just spent a lot of time shaking my head at bugs I’ve had to fix for clients because they shipped code that depends on an alpha/beta version of a library or a polyfill for a standard that hasn’t been finalized yet, so I’m sensitive to these kinds of decisions.
At the end of the day, I’m just a little frustrated at realizing the API for custom elements is less friendly than I had thought (again, entirely my fault for not reading as closely as I should have). I also understand that this is well past the point where anyone is willing to rethink it.
(I also want to be clear that I really appreciate the work being done here by everyone on the working group. Obviously I would have liked this issue to turn out differently, but I’m not complaining that this is some horrible travesty. The big picture is still an improvement for the web.)
On the other hand, there is a lot of Babel usage out there (the majority of non-trivial JS codebases I’ve worked on as a consultant over the past year have used it), and I hadn’t really expected that I’d need such an awkward and specialized method for creating a custom element with it.
Okay. If you don't like a method, you can also define a specialized super class shown below. Obviously, this particular version of BabelHTMLElement only works with a browser engine with both ES6 and custom elements support but you can make it work with whatever polyfill as well.
Note that you can be more sleek with something like this (although I highly discourage you to override the native HTMLElement interface like this but sooner or later someone is gonna realize and do it so I'm gonna leave it here).
HTMLElement=(function(OriginalHTMLElement){functionBabelHTMLElement(){if(typeofReflect=='undefined'||typeofReflect.construct!='function'||typeofcustomElements=='undefined'){// Use your favorite polyfill.}constnewTarget=this.__proto__.constructor;returnReflect.construct(OriginalHTMLElement,[],newTarget);}Object.setPrototypeOf(BabelHTMLElement,OriginalHTMLElement);Object.setPrototypeOf(BabelHTMLElement.prototype,OriginalHTMLElement.prototype);returnBabelHTMLElement;})(HTMLElement);classMyElementextendsHTMLElement{constructor(){super();this._id=1;}}customElements.define('my-element',MyElement);
@WebReflection: In the case, you're still looking for a solution that works in both Babel + Polyfill and native ES6 + custom elements, see the comment above ^
I've verified that both the ES6 and the Babel transpiled version works. The key here is to directly invoke Reflect.construct in your polyfill and not rely on Babel's super() call which, as you pointed out, is broken.
I am able to use Reflect.construct to migrate all my V0 components to V1. But now I am facing issues while I am creating new V1 components using ES6 classes(babelified).
Seems like all children with ES6 classes custom components are getting initialized correctly by the time browser calls connectedCallback but non class based child components which are built using Reflect.construct technique are not initialized in the connectedCallback of parent(which is ES6 class based component). Anyone faced this issue?
Note that in general custom elements are upgraded in the tree order (prefix DFS), and connected & disconnected callbacks are enqueued in the tree order as well so in connectedCallback, for example, you shouldn't expect to be able to talk to your child elements / nodes. The recommended paradigm is for each child custom element to notify its parent. If you had to observe other kinds of changes or have to accept non-custom element, the recommendation is to use MutationObserver.
The recommended paradigm is for each child custom element to notify its parent.
Is there any implementation or wrapper to handle after my child components are ready and attached? MutationObserver is not something which completes my requirement. At present I am having setTimeOut in my base component class like following which runs ready method only once. But this doesn't seem to be correct way.
connectedCallback() {
if (!this.isCustomInitialized) setTimeOut(()=>{this.ready()}, 0);
this.isCustomInitialized = true;
}
ready() {
// write code which runs after children custom components are ready with shadowDOM
}
MutationObserver is not something which completes my requirement
Why? MutationObserver lets you observe when child elements are inserted or removed. That's exactly when you should be running the logic to update your container element.
In general, the idea of all children being ready is flawed since an element can be inserted or removed dynamically.
I got your point of element insertion or removal but there is high probability the MutationObserver runs multiple times if there are multiple children that are active. Anyways I don't think need for lifecycle event after children being ready is a flaw. Even many major frameworks react (componentDidMount), vue (mounted), angular (afterView/afterContent) provides you similar functionality.
Even many major frameworks react (componentDidMount), vue (mounted), angular (afterView/afterContent) provides you similar functionality.
componentDidMount is the same as connectedCallback right? it has no concept of completeness of rendering.
Consider the following example:
// connected to dom =>
<foo-bar></foo-bar>
// is it ready?
// no as it needs to load some language data from an api =>
<foo-bar><p>hey foo</p></foo-bar>
// new we could say it's ready
It seems only you as a component author can make sure you are done done... and you can use whatever means necessary to do so (Promise, Callback, Event, ...)
PS: even more "crazy" example... assuming we take the finished loading of translations as being ready: an element that loads a form from an api which has some translations but also special input elements which need to load their own translations => so you could end up with the "form" being already ready (e.g. translations loaded) but the child elements are not (e.g. translations are not yet loaded) ... so to be truly ready you would need to check for every child elements readiness as well... => we are going down the rabbits hole 🙈
I haven't seen how react works internally much 😁 . I just tried logging componentDidMount and found that children's componentDidMount are called before parent's. But I am assuming due to virtual DOM that comes in react life cycle events, componentDidMount is happening after child DOM ready and connected as well.
Regarding example, true that it is getting complicated. I think that's where frameworks like react or vue won the game over custom elements/webcomponents.
@thecodejack
You have to be aware of one major caveat of custom elements, they are asynchronous. React is synchronous.
@daKmoR
So you can't say connectedCallback is the same as componentDidMount, there is this big difference of asynchronicity and others like V-DOM vs. the DOM.
@AndyOGo thx for clearing that up... so for the rendering componentDidMount will be sync.
However, the example with the loading of translations (via fetch to an external api server) is still valid right? or will componentDidMount wait until my xhr is done?
Regarding loading of translations, yes it's still valid.
Normally the workflow is as follows on componentDidMount() -> fetch() some data -> then call setState() -> will trigger componentDidUpdate, etc.
@domenic@morewry I just want to pop in and say that this is ridiculous. Javascript is a prototype-based language, and to get "consensus" it was necessary to block a critical feature from being possible using the prototype paradigm--and you refuse to fix it when it causes problems because of some sort of politics? It's nice that we have the option to use nice ES6 class syntax... but it's less powerful than raw prototyping and it's simply mind-boggling to me that my search for a solution led me to "there is none, because a committee banned it to force you to code their way even though the core features you're using aren't going anywhere".
Sometimes features are deprecated and things aren't backwards compatible; sometimes poor design introduces obstacles; but "sometimes a committee wants everyone to code a certain way for no particular reason and introduces obstacles to using perfectly supported features in a given context on purpose"? Is Javascript deprecated? Or is there a plan to eliminate prototypes from the language and move to a purely class-based approach--then perhaps add in strong typing and C++ template syntax? If so, that's crazy; if not, then the lack of support for prototype-based custom components is a serious bug.
Is there no way this can be brought up and addressed now that 4 years have passed? If the only reason there's no solution is that the design was intentionally crippled by bureaucratic fiat at a face-to-face meeting half a decade ago, perhaps it could be fixed now?
I guess I will now proceed to implement a bizarre, unreadable, inefficient workaround or introduce some hideous class definitions into my otherwise class-free library, for no reason whatsoever except that "the owners of the internet don't like prototypes".
Reacted by Andreas, Rhy Moore, DeloreanAB, Tim Breitkreutz, Michael Gutbier, Luis Melo, Renie Siqueira, Ibrahim Tanyalcin, Rizky Muhammad Baihaqy, The Jared Wilcurt and 2 more
Reacted by Hybrid Web Dev
Reacted by Rhy Moore, Mark, DeloreanAB and Denys Ivanchuk
Reacted by Kim Helberg
Reacted by Rhy Moore, Michael Gutbier, The Jared Wilcurt, Kim Helberg and ducky
@sapphous Class syntax is itself part of the “prototype paradigm,” but as others have mentioned, you don’t actually have to use it:
functionNoClassSyntaxElement(){returnReflect.construct(HTMLElement,[],new.target);// or Reflect.construct(HTMLElement, [], NoClassSyntaxElement), I suppose, if you don’t care about subclassing}NoClassSyntaxElement.__proto__=HTMLElement;NoClassSyntaxElement.prototype.__proto__=HTMLElement.prototype;customElements.define('no-class-syntax',NoClassSyntaxElement);console.log(newNoClassSyntaxElement().matches(':defined'));// true
This is useful if you need the super reference to be static but need to leave [[IsExtensible]] true (i.e., mimicking the behavior of platform interface constructors that inherit from others). That is pretty niche, mainly of interest for high-fidelity polyfilling stuff, but if for whatever reason you are averse to using class syntax to define your prototypes and object factories, it could serve you well too, and can be tucked away in a helper function.
Class syntax is a mechanism for defining prototypes declaratively alongside any initialization work involved in minting new instances. It doesn’t prevent manipulating the prototype and its properties; you can still do everything you might do in its absence. There is one (fairly obscure) primitive capability that class syntax has that is not exposed any other way* and there is one (fairly obscure) primitive capability that function syntax has that class syntax doesn’t**, but apart from these two things, they are exactly equal in capability.
Not trying to convince you to use em, just mentioning this because “less powerful than raw prototyping” seems like it might be a misconception (if you meant powerful in the sense of what fundamental capabilities they permit).
Not super important, but if curious, the two capability disconnects are...
* Class syntax is the only way to define a function whose [[ConstructorKind]] is "derived", which means you can skip or defer the initial access of new.target.prototype, which is technically observable. This is not possible with function syntax unless you count using a Proxy construct trap to achieve it.
** Function syntax allows defining a function that has [[Construct]] but whose prototype property is initially writable, while class syntax does not.
When private fields land, the situation will change — that’s a pretty major primitive capability which, at least initially, will only be exposed through class syntax.
Reacted by Giwayume, Rhy Moore, Ryosuke Niwa, Logan Moore, Joshua Brinsfield, Vlado Chabal, Joe Pea, Michael Gutbier, Michiel Crefcoeur, Philipp Mossier and 3 more
Activity
domenic commentedon Oct 15, 2016
It's not possible to use custom elements without ES6 classes. That was a design decision necessary to achieve consensus at the January face-to-face meeting.
Closing, since there's nothing actionable here, but happy to continue discussing in the closed thread.
domenic commentedon Oct 15, 2016
I've updated the linked documentation to at least be up to date with custom elements v1. It still is on old shadow DOM however, and in general https://developer.mozilla.org/en-US/docs/Web/Web_Components looks very, very outdated and confusing. If anyone has time to update all the web components docs to the latest specs, that would be a great help to developers everywhere, I am sure.
rektide commentedon Oct 15, 2016
I'd like to see non-class based JS become possible, hopefully in v2. Please re-open this as a request. Classes are syntax, statically constructed, which means we can't create components on the fly with code. This is a serious and frankly scary limitation.
For an example of use, if I wanted to generate components for, say, schema.org, the class based syntax means that I have to manually type out class definitions for ~650 components. Having normal, regular JS objects would have let me use code to generate new components. Please re-open this as an outstanding issue @domenic.
domenic commentedon Oct 15, 2016
Sorry, this was a condition of getting consensus, and is now fully built in to the architecture of the feature. It cannot be changed.
domenic commentedon Oct 15, 2016
I hope you're aware that you can generate classes dynamically just as easily as functions, so you certainly would not need to type those out. Classes are just as much syntax as functions are. If you don't know how do do this, please ask on StackOverflow, but not here.
rniwa commentedon Oct 15, 2016
FWIW, you can use
Reflect.constructto callHTMLElement's constructor. e.g.Mr0grog commentedon Oct 16, 2016
(Apologies if this issue has already been discussed elsewhere; I had entirely failed to consider it before and I haven’t seen it mentioned…)
Will this cause problems for existing JS codebases that use a WebComponents polyfill with a transpiler like Babel? For example, transpiling this code using Babel’s
es2015preset fails to work because the resulting JS doesn’t useReflect.construct:I understand that native custom elements won’t be available in browsers that don’t already support ES-2015 class syntax, but if someone is using Babel + a polyfill for web components, it seems like they’d have a situation where their code works in older browsers (because the polyfill is active), but not in newer ones (because the polyfill just defers to the native implementation). That seems like a pretty big practical problem, but is it one you are concerned about here?
rniwa commentedon Oct 16, 2016
It is true that if you're using Babel and polyfill, then the above code won't work out-of-box but that's true of any polyfill that got written before the standard is finalized.
There are various ways to workaround such issues, and probably the simplest solution is to wrap the thing you pass to
customElements.definewith a class. e.g.Obviously, this leaves
new TestElementnon-functional. An alternative approach is to replacesuper()call inTestElementby something special like:with
There are dozens of other ways to cope with this limitations and that's really up to framework and library authors.
On a broader note, I don't think the standards process or API design in standards should be constrained by polyfills written before the general consensus on the API shape has been reached and at least two major browser engines have implemented it. Also, deploying a polyfill on production before the standards have become stable is almost always a bad idea.
Mr0grog commentedon Oct 17, 2016
I suppose I was really most focused here on the impact to existing codebases. It’s not as if that hasn’t been a consideration in other web standards, though I do understand that current usage of polyfills for custom elements (and especially v1-esque polyfills) is quite small.
On the other hand, there is a lot of Babel usage out there (the majority of non-trivial JS codebases I’ve worked on as a consultant over the past year have used it), and I hadn’t really expected that I’d need such an awkward and specialized method for creating a custom element with it. It may be further complicated in trying to find solutions that allow someone to inherit from a custom element provided as a third-party module, where the provider of the component may have solved the issue in their own way. As you noted, there are many ways to work around it.
I agree! I’ve just spent a lot of time shaking my head at bugs I’ve had to fix for clients because they shipped code that depends on an alpha/beta version of a library or a polyfill for a standard that hasn’t been finalized yet, so I’m sensitive to these kinds of decisions.
At the end of the day, I’m just a little frustrated at realizing the API for custom elements is less friendly than I had thought (again, entirely my fault for not reading as closely as I should have). I also understand that this is well past the point where anyone is willing to rethink it.
(I also want to be clear that I really appreciate the work being done here by everyone on the working group. Obviously I would have liked this issue to turn out differently, but I’m not complaining that this is some horrible travesty. The big picture is still an improvement for the web.)
rniwa commentedon Oct 17, 2016
Okay. If you don't like a method, you can also define a specialized super class shown below. Obviously, this particular version of
BabelHTMLElementonly works with a browser engine with both ES6 and custom elements support but you can make it work with whatever polyfill as well.rniwa commentedon Oct 17, 2016
Note that you can be more sleek with something like this (although I highly discourage you to override the native
HTMLElementinterface like this but sooner or later someone is gonna realize and do it so I'm gonna leave it here).rniwa commentedon Oct 17, 2016
@WebReflection: In the case, you're still looking for a solution that works in both Babel + Polyfill and native ES6 + custom elements, see the comment above ^
WebReflection commentedon Oct 17, 2016
@rniwa thanks for mentioning me but I'm not sure it's so easy.
Babel is plain broken when it comes to super calls and my poly already patches
HTMLELement, so does the one from googlers.I strongly believe this should be solved on Babel side, otherwise we're blocking and degrading native performance because of tooling on our way.
Tooling should improve and help, not be a problem.
rniwa commentedon Oct 17, 2016
I've verified that both the ES6 and the Babel transpiled version works. The key here is to directly invoke
Reflect.constructin your polyfill and not rely on Babel'ssuper()call which, as you pointed out, is broken.72 remaining items
thecodejack commentedon Dec 18, 2018
I am able to use Reflect.construct to migrate all my V0 components to V1. But now I am facing issues while I am creating new V1 components using ES6 classes(babelified).
Seems like all children with ES6 classes custom components are getting initialized correctly by the time browser calls
connectedCallbackbut non class based child components which are built usingReflect.constructtechnique are not initialized in theconnectedCallbackof parent(which is ES6 class based component). Anyone faced this issue?rniwa commentedon Dec 18, 2018
Note that in general custom elements are upgraded in the tree order (prefix DFS), and connected & disconnected callbacks are enqueued in the tree order as well so in
connectedCallback, for example, you shouldn't expect to be able to talk to your child elements / nodes. The recommended paradigm is for each child custom element to notify its parent. If you had to observe other kinds of changes or have to accept non-custom element, the recommendation is to use MutationObserver.thecodejack commentedon Dec 19, 2018
Is there any implementation or wrapper to handle after my child components are ready and attached? MutationObserver is not something which completes my requirement. At present I am having
setTimeOutin my base component class like following which runsreadymethod only once. But this doesn't seem to be correct way.rniwa commentedon Dec 20, 2018
Why? MutationObserver lets you observe when child elements are inserted or removed. That's exactly when you should be running the logic to update your container element.
In general, the idea of all children being ready is flawed since an element can be inserted or removed dynamically.
thecodejack commentedon Dec 20, 2018
I got your point of element insertion or removal but there is high probability the MutationObserver runs multiple times if there are multiple children that are active. Anyways I don't think need for lifecycle event after children being ready is a flaw. Even many major frameworks react (componentDidMount), vue (mounted), angular (afterView/afterContent) provides you similar functionality.
daKmoR commentedon Dec 20, 2018
componentDidMount is the same as connectedCallback right? it has no concept of completeness of rendering.
Consider the following example:
It seems only you as a component author can make sure you are done done... and you can use whatever means necessary to do so (Promise, Callback, Event, ...)
PS: even more "crazy" example... assuming we take the finished loading of translations as being ready: an element that loads a form from an api which has some translations but also special input elements which need to load their own translations => so you could end up with the "form" being already ready (e.g. translations loaded) but the child elements are not (e.g. translations are not yet loaded) ... so to be truly ready you would need to check for every child elements readiness as well... => we are going down the rabbits hole 🙈
thecodejack commentedon Dec 20, 2018
I haven't seen how react works internally much 😁 . I just tried logging
componentDidMountand found that children'scomponentDidMountare called before parent's. But I am assuming due to virtual DOM that comes in react life cycle events,componentDidMountis happening after child DOM ready and connected as well.Regarding example, true that it is getting complicated. I think that's where frameworks like react or vue won the game over custom elements/webcomponents.
AndyOGo commentedon Dec 20, 2018
@thecodejack
You have to be aware of one major caveat of custom elements, they are asynchronous. React is synchronous.
@daKmoR
So you can't say
connectedCallbackis the same ascomponentDidMount, there is this big difference of asynchronicity and others like V-DOM vs. the DOM.I really recommend to carefully study the whole spec to become familiar with all the caveats involved:
https://html.spec.whatwg.org/multipage/custom-elements.html
daKmoR commentedon Dec 20, 2018
@AndyOGo thx for clearing that up... so for the rendering
componentDidMountwill be sync.However, the example with the loading of translations (via fetch to an external api server) is still valid right? or will
componentDidMountwait until my xhr is done?AndyOGo commentedon Dec 20, 2018
@daKmoR
You are welcome.
Regarding loading of translations, yes it's still valid.
Normally the workflow is as follows on
componentDidMount()->fetch()some data -> then callsetState()-> will triggercomponentDidUpdate, etc.I really like this interactive diagram for React Lifecycle hooks and would wish similiar for custom elements:
http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
xgqfrms commentedon Jul 3, 2020
what's wrong with that?
https://codepen.io/xgqfrms/pen/MWKrXpe?editors=1111
WebReflection commentedon Jul 3, 2020
@xgqfrms that's not a valid custom element name
xgqfrms commentedon Jul 3, 2020
@WebReflection Thanks a lot, I had fixed that.
👍 customElements.define("ufo-element", UFO);
👎 customElements.define("ufo", UFO);
sapphous commentedon Dec 2, 2020
@domenic @morewry I just want to pop in and say that this is ridiculous. Javascript is a prototype-based language, and to get "consensus" it was necessary to block a critical feature from being possible using the prototype paradigm--and you refuse to fix it when it causes problems because of some sort of politics? It's nice that we have the option to use nice ES6 class syntax... but it's less powerful than raw prototyping and it's simply mind-boggling to me that my search for a solution led me to "there is none, because a committee banned it to force you to code their way even though the core features you're using aren't going anywhere".
Sometimes features are deprecated and things aren't backwards compatible; sometimes poor design introduces obstacles; but "sometimes a committee wants everyone to code a certain way for no particular reason and introduces obstacles to using perfectly supported features in a given context on purpose"? Is Javascript deprecated? Or is there a plan to eliminate prototypes from the language and move to a purely class-based approach--then perhaps add in strong typing and C++ template syntax? If so, that's crazy; if not, then the lack of support for prototype-based custom components is a serious bug.
Is there no way this can be brought up and addressed now that 4 years have passed? If the only reason there's no solution is that the design was intentionally crippled by bureaucratic fiat at a face-to-face meeting half a decade ago, perhaps it could be fixed now?
I guess I will now proceed to implement a bizarre, unreadable, inefficient workaround or introduce some hideous class definitions into my otherwise class-free library, for no reason whatsoever except that "the owners of the internet don't like prototypes".
bathos commentedon Dec 2, 2020
@sapphous Class syntax is itself part of the “prototype paradigm,” but as others have mentioned, you don’t actually have to use it:
This is useful if you need the super reference to be static but need to leave [[IsExtensible]] true (i.e., mimicking the behavior of platform interface constructors that inherit from others). That is pretty niche, mainly of interest for high-fidelity polyfilling stuff, but if for whatever reason you are averse to using
classsyntax to define your prototypes and object factories, it could serve you well too, and can be tucked away in a helper function.Class syntax is a mechanism for defining prototypes declaratively alongside any initialization work involved in minting new instances. It doesn’t prevent manipulating the prototype and its properties; you can still do everything you might do in its absence. There is one (fairly obscure) primitive capability that class syntax has that is not exposed any other way* and there is one (fairly obscure) primitive capability that function syntax has that class syntax doesn’t**, but apart from these two things, they are exactly equal in capability.
Not trying to convince you to use em, just mentioning this because “less powerful than raw prototyping” seems like it might be a misconception (if you meant powerful in the sense of what fundamental capabilities they permit).
Not super important, but if curious, the two capability disconnects are...
* Class syntax is the only way to define a function whose [[ConstructorKind]] is "derived", which means you can skip or defer the initial access of
new.target.prototype, which is technically observable. This is not possible withfunctionsyntax unless you count using aProxyconstruct trap to achieve it.** Function syntax allows defining a function that has [[Construct]] but whose
prototypeproperty is initiallywritable, while class syntax does not.When private fields land, the situation will change — that’s a pretty major primitive capability which, at least initially, will only be exposed through class syntax.