小编Mah*_*ber的帖子

如何分辨javascript中哪个键称为函数

如果我有一个带有多个调用同一功能的键的对象,并且该功能在其作用域之外实现,那么如何确定哪个键调用了此功能?例如:

function tellYourAge() {
   return function()
   {
       // I already know here that this refers to Population
       // For example, console.log(this) will print the Population object
   }
}

{
   let Population = {
     Mahdi: tellYourAge(),
     Samuel: tellYourAge(),
     Jon: tellYourAge()
   };

   Population.Mahdi(); // It should log 18
   Population.Samuel(); // It should log 20
   Population.Jon(); // It should log 21
}
Run Code Online (Sandbox Code Playgroud)

javascript function object

6
推荐指数
1
解决办法
301
查看次数

标签 统计

function ×1

javascript ×1

object ×1