This is the first in a series of posts about core concepts of hybrids - a library for creating Web Components with simple and functional API.
ES20...
For further actions, you may consider blocking this person and/or reporting abuse
Well done, exploring a more functional approach!
There may be an issue with your use of the word "pure," both in the title of this article and in the hybrid docs. Either it's unclear how
hybrids
uses pure functions, or the term "pure" isn't accurate and is best left out. As it is, the term left me confused, and I'll attempt to explain why.Per Wikipedia and others, a function must meet two criteria to be pure: idempotency (which you have) and 0 side effects.
Here's the first function used as an example in the Hybrid docs:
This function isn't pure because it mutates
host
. Here's one way to get similar functionality from a pure function:Or, perhaps more usefully…
In your place, I'd either remove the term "pure", or update the docs to explicitly demonstrate how
hybrids
takes advantage of functional purity.None of this should be taken as feedback on the system itself. The approach you offer is intriguing. Bravo!
Thanks for the comment! In hybrids pure function term relates mainly to the property descriptor methods (get and set), where you don't mutate host element - it is only used to get dependencies.
Obviously,
connect
method is not pure - it is created especially for side effects, like adding event listeners. However, for the user of the library, the definition can be a simple object with values and pure functions - and usually, it is, when you use built-in factories or created by yourself.The
increaseCount
from the example is a side effect in some way (not technically) - it is a callback attached to the button - it is not an integral part of the definition of the custom element.This is the first sentence of the docs. As you can see, it means, that library favors pure functions, not require to use them. Also, there is no statement, that all of the functions should be pure :)
Ah, that last point is subtle. Some of my confusion came from having skimmed over the code samples in the docs trying to find an example that showed pure functions in use, and didn't find any.
Not a complaint, mind you. Just sharing a perspective in case you feel it makes sense to more explicitly demonstrate the ways in which hybrids promotes the use of pure functions. The other part — about how it demotes
class
andthis
syntax — is clear from the docs as-is…and that's something I really like about what you've done!Thanks for your thoughtful reply. :)
Your last example.
I think you just put my example in the file and run it ;) Then of course
fullName
will not work - it is just a function that requires a host as an argument. The library is doing that when it defines properties - this example uses translation feature, so the final definition (if you pass this todefine('my-element', MyElement)
) will be:Then your custom element will have
element.fullName
property, which works and returns concatenated first and last name.I appreciate the answering, however it's still failing!
I'm trying to run this in Node v12
From the looks of it you are using RequireJS? How would a pure JavaScript implementation look like?
My code is not fully working example ;) It's a most important part of the file. You still need to import the library. If you use some kind of bundler you can use this:
If you want "pure" JS solution, you can use ES modules:
Or for older browsers support:
Read more in Getting Started section of the library documentation.
Awesome, love it. Hope to see this project go far!
Thanks! I have a lot of cool ideas, so stay tuned for the updates!
Hmm to be honest i dont understand step 4, to get rid of "this", there must be something more that allows using current instance as first argument of setter, the glue code seems to be missing.
The change is simple. You can think of this keyword as a different type of argument of the function. Instead of using it, we can expand the arguments list by passing the context (in actual getter/setter property definition) as the first argument (shifting rest of them).
If you want to know more, I recommend to see the implementation in the library. For example here: github.com/hybridsjs/hybrids/blob/...