如果我有代码:
function A() {
function B() {
}
B();
}
A();
A();
Run Code Online (Sandbox Code Playgroud)
是每次调用A时都解析并创建B函数(这样可以降低A的性能)?
如果你只想在内部使用一个函数,那么闭包怎么样。这是一个例子
var A = (function () {
var publicFun = function () { console.log("I'm public"); }
var privateFun2 = function () { console.log("I'm private"); }
console.log("call from the inside");
publicFun();
privateFun2();
return {
publicFun: publicFun
}
})();
console.log("call from the outside");
A.publicFun();
A.privateFun(); //error, because this function unavailable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |