代码改变世界

javaScript杂货铺

2010-10-23 18:25  BlueDream  阅读(601)  评论(0)    收藏  举报

函数分析

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.';
  }
};

function tester() {
    alert(
'this is a tester');
}
alert(Block(tester, 'body'))