Overriding Function Type prototypes

后端 未结 1 646
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 17:11

I have come across an interesting behavior when playing around and trying to override Function prototype.

Let\'s assume we have overriden toString() lik

相关标签:
1条回答
  • 2020-12-30 17:31

    This is a problem of a missing semicolon:

    Function.prototype.toString = function() { 
        …
    }; /*
     ^ */
    (function foo(){}).toString();
    

    Otherwise it is interpreted as

    Function.prototype.toString = function(){…}(function foo(){}).toString();
    

    which calls the function expression that is supposed to override toString like an IIFE

    … (function(){…}(function foo(){})) …
    

    …in the global context, not on a function.

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