函数内部的jquery函数

dar*_*ryl 11 javascript function

是否可以在另一个函数中使用这样的函数?

function foo() {

    // do something

    function bar() {

        // do something

    }

    bar();

}

foo();
Run Code Online (Sandbox Code Playgroud)

Mrc*_*ief 17

是的,你可以这样做.bar外面的任何人都看不到foo.

你可以bar在里面打电话foo:

function foo() {

    // do something

    function bar() {

        // do something

    }
    bar();

}
Run Code Online (Sandbox Code Playgroud)


Der*_*會功夫 5

是的你可以.
或者你可以这样做,

function foo(){

    (function(){

        //do something here

    })()

}
Run Code Online (Sandbox Code Playgroud)

或这个,

function foo(){

    var bar=function(){

        //do something here

    }

}
Run Code Online (Sandbox Code Playgroud)

或者你希望功能"bar"是通用的,

function foo(){

    window.bar=function(){

        //something here

    }

}
Run Code Online (Sandbox Code Playgroud)

跳这有助于你.