如果我们有:
function parent(){
function a(){
return 'i am a';
}
function b(){
return 'i am b';
}
function c(e,f){
console.log(e+f);
}
c(a(),b());
}
任何检索嵌套函数名称的内置方法:a,b,c。让我们说:
parent.closures()
// ['a','b','c']
}
答案 0 :(得分:0)
Function.prototype.closures=function() {
var that=this,fn = function(r) {
//see demo==> to reduce non-functional code
};
return this.toString().split("\n").filter(function(d) {
return d.indexOf('function ') !== -1
}).map(fn).filter(function(f) {
return f !== that.name;
})
};
然后:
parent.closures()
// ['a','b','c']