Making an object non-callable

前端 未结 4 1490
眼角桃花
眼角桃花 2020-12-04 02:59

In JavaScript, functions are callable.

Can I remove this attribute from a function, leaving only a normal object?

var foo = function () {};
foo.[[ca         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 03:53

    this isn't an elegant solution, the idea is to create the "enabled" attribute as a flag to know if the function is enabled:

    var foo = function () { 
      if(!arguments.callee.enabled){
        throw "function disabled";
      }
      console.log("Hello");
    }
    
    foo.enabled = true;
    foo();
    foo.enabled = false;
    foo();
    

提交回复
热议问题