[英]Is there a way to exploit the performance advantages of using prototype methods in JavaScript factory functions?
[英]Advantages of using inner functions(javascript)
這可能不是一個非常具體的問題,但是我想知道使用內部函數的優點是什么? 我最近開始閱讀閉包(javascript),它們總是引用內部函數。
var pet = function(name) {
var getName = function() {
return name;
}
return getName;
}
myPet = pet('Vivie');
myPet();
為什么我們不想分離getName函數並引入一個'name'參數,以便我們可以獨立地調用它?
var pet = function(name){
return getName();
}
function getName(name){
return name;
}
謝謝,我對javascript很陌生
試試這篇文章。 理解JavaScript中的閉包的簡單指南
我在這里復制了本文解釋的部分代碼。 運行此代碼片段以查看內部函數的行為。
<script> function outer() { var b = 10; var c = 100; function inner() { var a = 20; console.log("a= " + a + " b= " + b); a++; b++; } return inner; } var X = outer(); // outer() invoked the first time var Y = outer(); // outer() invoked the second time //end of outer() function executions X(); // X() invoked the first time X(); // X() invoked the second time X(); // X() invoked the third time Y(); // Y() invoked the first time </script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.