循环遍历函数对象并向对象中的每个函数添加一个新方法

Loop through an object of functions & add a single, new method to each single function within the Object

我有一个 functionsobject 并且我需要添加一个新的、单一的 method 称为 log(它打印 methodObject 中的每个 function 在运行时动态地分配给每个 function

例如,假设这是我的动态 object of functions:

let actions = { 
  theBoringFunction() { 
    return 'function 1'
  },

  anotherBoringFunction() {
    return 'function 2'
  },

  theMostBoringFunction() {
    return 'function 3'
  },
}

我可以像这样单独向方法添加新的 function

actions.anotherBoringFunction.log = function () {  
  return console.log(this.name)
}

actions.anotherBoringFunction.log() // logs anotherBoringFunction

它就像一个魅力,但我在 actions object 中有很多 functions,我需要为所有这些添加 log()

我试过遍历 object 并像这样添加 method

// won't work key is typeof String :(
for (let key in actions) { 
  key.log = function () { 
    return console.log(this.name)
  }
}

显然,它不起作用,因为 keystring 的一种类型,所以它不能向其添加 method。我怎样才能完成我正在寻找的东西?我一直在尝试 proxies,但到目前为止也没有成功。

编辑编号 1:

我添加了一个可重现的示例 here.

let actions = { 
  theBoringFunction() { 
    return 'function 1'
  },

  anotherBoringFunction() {
    return 'function 2'
  },

  theMostBoringFunction() {
    return 'function 3'
  },
}

// won't work key is typeof String :(
/* for (let key in actions) { 
  key.log = function () { 
    return console.log(this.name)
  }
} */

actions.anotherBoringFunction.log = function () {  
  return console.log(this.name)
}

actions.anotherBoringFunction.log() // logs anotherBoringFunction

for (let key in actions) { 
  actions[key].log = function () { 
    return console.log(this.name)
  }
}