Javascript嵌套函数 - 有什么区别?

Sha*_*oni 2 javascript function

我在思考以下Javascript最佳实践模式.我的代码中有一个函数,它有一些嵌套函数.应该首选以下哪种模式?为什么?

function parent() {
    function child1() {
        //child 1 code
    }

    function child2() {
        //child2 code
    }
    //parent code
    return {
        child1: child1,
        child2: child2
    };
}
Run Code Online (Sandbox Code Playgroud)

要么

function parent() {
    var child1 = function () {
        //child 1 code
    };
    var child2 = function () {
        //child2 code
    };
    //parent code
    return {
        child1: child1,
        child2: child2
    };
}
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 5

这两个版本之间的主要区别在于

var a = function(){ ...
Run Code Online (Sandbox Code Playgroud)

函数是undefined直到这一行(但变量存在)而在

function a() { ...
Run Code Online (Sandbox Code Playgroud)

自封闭函数开始以来定义了该函数:

console.log(a); // logs "function a(){ return 2} "
console.log(b); // logs "undefined"
function a(){ return 2};
var b = function(){ return 2};
Run Code Online (Sandbox Code Playgroud)

正如Jan指出的那样,第一个版本也定义name了函数的属性.

除此之外,没有区别,主要是风格问题,没有我所知道的最佳实践.