javascript bind callback to any function -
i bind or chain callback/function function.
there similar question (without valid answer): jquery , bind callback function
but, me not want limited jquery realm. looking broader answer, wonder if it's possible vanilla javascript or library other jquery.
example:
// function don't want or can't edit it's body var thirdpartyobject = { dosomething: function(args) { // lot of code console.log('doing stuff: ' + args); } }; // function var easycallback = function() { // processing stuff console.log('doing more stuff'); } // bind magiclibrary.bind(thirdpartyobject.dosomething, easycallback); // run thirdpartyobject.dosomething(1); thirdpartyobject.dosomething(10);
when run "code", following output represents behaviour i'm looking for:
doing stuff: 1 doing more stuff doing stuff: 10 doing more stuff
is possible?
edit: bind
conceptual term, maybe think chain
, trigger
or term.
but import second function or callback in example easycallback()
must somehow connected first 1 dosomething()
.
and every time dosomething()
called or executed want easycallback()
executed after first finished.
but without "wrapping" them around or without rewriting first one.
you have wrap dosomething
function inside yet function, so:
// function don't want or can't edit it's body var thirdpartyobject = { dosomething: function(args) { // lot of code console.log('doing stuff: ' + args); } }; // function var easycallback = function() { // processing stuff console.log('doing more stuff'); } // bind // magiclibrary.bind(thirdpartyobject.dosomething, easycallback); const dosomething = thirdpartyobject.dosomething.bind(thirdpartyobject); thirdpartyobject.dosomething = function(args) { dosomething(args); easycallback(args); }; // run thirdpartyobject.dosomething(1); thirdpartyobject.dosomething(10);
Comments
Post a Comment