这里是我做了测试的例子:是一个全局范围的函数内的JavaScript函数?
function f1(var1){
alert('f1 called');
function f2(var2){
alert('f2 called');
}
this.data='something else';
this.f2 = f2;
}
f1.data='something';
f1.f3 = function(var3){
alert('f3 called');
}
console.log(f1); //shows the function
console.log(f1.data); //can have properties attached - cool
console.log(f1.f2(2)); //error: "f1.f2" is not a function
console.log(f1.f3(3)); //this works as expected
看来,F1里面的功能f2
在范围上本地。是否有任何等效的方法来调用这样的函数内的函数?
其分配是这样的: 'this.f2 = function(){// ...'! –
您需要将其暴露给全局范围,例如,使用任何一种JS模块模式。但是,在本地确定一个功能的关键在于*避免*污染全球范围。你最终的目标是什么? –
@ibrahimmahrir \t ...这就是'this.f2 = f2'所能做的;没有区别。 –