How to "append" an action to a function?

I wanted to create a function that I can add actions to later. I tried this:

Function.prototype.appendAction = function(action) {
  let _ogFn = this
  return function(...args) {
    _ogFn(...args)
    action()
  }
}
function a() {
  console.log("foo")
}
a()
a = a.appendAction(() => console.log("bar"))
a()

This does work, but how can I make it change the function automatically? I want to make it work like this:

a()
a.appendAction(() => console.log("bar")) //note it doesn't have the "a = "
a() //changed function


from Recent Questions - Stack Overflow https://ift.tt/3EHWhQa
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)