[removed] is given function empty?

后端 未结 6 1808
日久生厌
日久生厌 2020-12-03 07:50

Let\'s have a function call

function doSomethingAndInvokeCallback(callback){
    // do something
    callback();
}

I can check if given arg

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 08:18

    It seems that you can define a function to retrieve the body of a function(1). I wrote a small (non-definitive) test of this:

    http://jsfiddle.net/6qn5P/

    Function.prototype.getBody =
      function() {
        // Get content between first { and last }
        var m = this.toString().match(/\{([\s\S]*)\}/m)[1];
        // Strip comments
        return m.replace(/^\s*\/\/.*$/mg,'');
      };
    
    function foo() {
        var a = 1, b = "bar";
        alert(b + a);
        return null;
    }
    
    console.log(foo.getBody());
    console.log(foo.getBody().length);
    

提交回复
热议问题