When extending the functionality of Knockout, when is it appropriate to use custom bindings vs extenders vs custom functions using "fn"? Are there performance implications when choosing one over another?
You would generally want to choose custom bindings when the functionality that you are trying to accomplish involves both the UI and your data, so where you need access to an element and usually some data to control what to do with the element.
There is really not a big difference between extenders and adding to .fn
at this point. Extenders were added to KO slightly before shared functions were available via .fn
.
It is usually not an issue, but extenders will just work for both observables and computed observables. Using .fn
you would need to add functions to ko.subscribable.fn
for it to be available to both. However, in some cases your functionality may only apply to one or the other, so using ko.observable.fn
and ko.computed.fn
would be a better choice.
I find extending .fn
to be the cleanest syntax. If you are sure to return this
or a new type if you are wrapping it, then you can support chaining easily. Extenders are a little more explicit and ensure that the API surface areas of the types remain lean (for what that is worth).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With