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 this JavaScript feature.
Update:
I've been asked to provide 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()
from Should I worry about conflicts with possible future ECMAScript changes if I want to add a property to a Function object with a generic name?
No comments:
Post a Comment