> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/12javascript-quan-gong-lve-ff1a-ke-fu-js-de-qi-guai-bu-fen/123wu-jian-yu-han-shu/12319kuang-jia-xiaoding-ning-ff1a-function-factories.md).

# 1.2.3.19.框架小叮嚀：Function Factories

* 利用[閉包](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/12317le-jie-bi-bao.html)的特性建立Function Factories
  * 由函數創造函數&#x20;
* 修改[重載](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/12311kuang-jia-xiao-ding-ning-ff1a-zhong-zai-han-shu.html)講到的程式碼
  * 會產生兩個不同的執行環境, 兩套記憶體
  * 當函數執行完後, 執行環境離開執行堆, 但記憶體都還在

```
function makeGreeting(language) {
    //將會回傳一個函數
    return (firstname, lastname){
        if (language === 'en') {
            console.log('Hello ' + firstname + " " + lastname)
        }
        if (language === 'es') {
            console.log('Hola ' + firstname + " " + lastname)
        }
    }
}

var greetEnglish = makeGreeting('en');
var greetSpanish = makeGreeting('es');

//invoke
greetEnglish('John', 'Doe');
greetSpanish('John', 'Doe');
```
