Ruby's Louvre

每天学习一点点算法

导航

javascript Function相关

 function Block(func, tag) {
    var _r_function = /^function\b\s*([\$\S]*)\s*\(/;
    var _r_codebody = /[^{]*\{([\d\D]*)\}$/;
    var _r_params = /[^\(]*\(([^\)]*)\)[\d\D]*/;
    tag = (tag || 'body').toLowerCase();
    with (func.toString()) {
    return (tag == 'body') ? replace(_r_codebody, '$1').replace(/^\s*|\s*$/g, '')
    : (tag == 'param') ? ((tag=replace(_r_params, '$1')) ? tag.split(/[,\s]+/) : [])
    : (tag == 'name') ? match(_r_function)[1]
    : (tag == 'scope') ? '[function (' +arguments.callee(func, 'param')+ ') {\n' +arguments.callee(func, 'body')+ '\n}][0]'
    : (tag == 'anonymous') ? Function.apply(this, arguments.callee(func, 'param').concat(arguments.callee(func, 'body')))
    : 'Block() with bad arguments.';
    }
    };
    var a = function () {
    alert(3333)
    };

    alert(Block(a, 'anonymous'))

取得函数名

var fname = function(f){
    return f.name;
}
if(!("name" in Function.prototype)){
    fname = function(f){
          return String(f).match(/^function\s*([^)]*)\(/)[1];
    }

}

posted on 2011-05-30 06:07  司徒正美  阅读(1632)  评论(1编辑  收藏  举报