Should I worry about conflicts with possible future ECMAScript changes if I want to add a generically named property to a Function object?
I am trying to create a function which can be invoked through the following ways:
log()
log.error()
Based on my understanding, we can creating such a function by writing on the function itself:
function log(){
}
log.error = () => {}
Is it not recommended writing on the function this way? Because JavaScript might release an update in the future which might hold the "error" property, then my "error" property will be overriding that JavaScript feature.
Here's an example of where people might be using this pattern and why it's useful.
For example, Jquery:
$( "#button-container button" ).on( "click", function( event ) {} )
and
$.ajax({ url: "/api/getWeather" });
you see this pattern in many popular libraries.
the $
is an object,
it's callable, so you can invoke it $('css query selector here')
and it's chainable, so you can call other methods on it $().on()
, and it has methods on it $.ajax()
Comments
Post a Comment