Determine if a JavaScript function is a bound function

前端 未结 7 1542
梦如初夏
梦如初夏 2020-12-14 16:51

Is there a way to determine if a JavaScript function is a bound function?

Example:

相关标签:
7条回答
  • 2020-12-14 17:23

    Based on previous answers, I create a function to determine:

    function isBoundFunction(func) {
        if(typeof func.prototype === 'object') return false
        try {
            new func()
        }
        catch(e) {
            return false
        }
        return true
    }
    

    This function determine three type of functions: 1. original function, whose prototype is object, 2. arrow function, which can not be used as constructor, 3. bound function.

    0 讨论(0)
提交回复
热议问题