Can you make an object 'callable'?

前端 未结 4 569
粉色の甜心
粉色の甜心 2020-12-03 04:37

Is it possible to make an object callable by implementing either call or apply on it, or in some other way? E.g.:

var obj = {};
obj         


        
4条回答
  •  生来不讨喜
    2020-12-03 05:18

    Following the same line of @Max, but using ES6 extensions to Object to pass all properties and prototype of an object obj to the callable func.

    Object.assign(func, obj);
    Object.setPrototypeOf(func, Object.getPrototypeOf(obj));
    

提交回复
热议问题