在另一个函数下运行函数的javascript

在另一个函数下运行函数的javascript,javascript,jquery,Javascript,Jquery,我有3个函数,第一个函数下面有第三个函数,我想在第二个函数中运行 function first () { function third () { } } function second () { third (); } 我怎样才能正确地第二次跑第三 谢谢。Javascript不会轻易允许这样做,为什么您首先需要这样做,您可能希望使用对象或原型来实现这一点 function first(){ third(); } function second(){ } f

我有3个函数,第一个函数下面有第三个函数,我想在第二个函数中运行

function first () {
    function third () {
    }
}

function second () {
    third ();
}
我怎样才能正确地第二次跑第三


谢谢。

Javascript不会轻易允许这样做,为什么您首先需要这样做,您可能希望使用对象或原型来实现这一点

function first(){
   third();
}

function second(){

}

function third(){
  second();
}

这取决于您希望如何设置第一个函数以及如何访问它。基本上,第三种方法对第一种是私有的。您需要在第一个上使用一个公共方法来调用第三个。该方法将由第二个调用

有很多方法可以做到这一点,我想到的一个方法是

编辑:我对参数的命名稍微好一点,这样它就不再是“param”了

function first()
{
    // define first's private
    var third = function(third_param)
    {
        alert(third_param);
    }

    // define first's public that calls the private
    this.callThird = function (call_third_param)
    {
        third(call_third_param);
    }

}

function second ()
{
    // get an instance of first
    var temp = new first();

    // call the public method on it
    temp.callThird('argument');
}
如果你不在乎第三者的隐私,你可以这样做

function first() { }

// public method
first.prototype.third = function(third_param)
{
    alert(third_param);
}


function second ()
{
    // get an instance of first
    var temp = new first();

    // call the public method on it
    temp.third('argument');
}
或者,这样,这也不是在使用私家车

function first()
{
    // the "this", makes it public
    this.third = function(third_param)
    {
        alert(third_param);
    }

}

function second ()
{
    // get an instance of first
    var temp = new first();

    // call the public method on it
    temp.third('argument');
}
如果您正试图只做一种名称空间类型的事情,那么您可以这样做(也没有私事)

但最好的方法可能是通过名称空间

var first = (function () {
    // define first's private function, store it in 'third', a private variable
    var third = function (third_param) {
        alert(third_param);
    };

    // return this object to the 'first' variable
    return { // define first's public that calls the private
        'callThird': function (call_third_param) {
            third(call_third_param);
        }
    };
} ());  // this line runs the function, closure'ing over the privates and 
// returning an object (that has access to the privates) to the 'first' variable

function second () {
    // usage:
    first.callThird("Hello World!");
}
为什么不将third()设为不嵌套在first()下的函数,然后让first()和second()调用third()

e、 g


您最好的选择是将两者通用的函数拉出,并在需要时调用它

var thirdf = function third () {
}

function first () {
  thirdf();
}

function second () {
  thirdf();
}

第一次返回第三次。如果您的示例更像这样,则此建议更有意义:

function first(foo) { // notice that foo is local to first function third() { // do something with foo in the outer scope } return third; } function second(bar) { bar(); // call whatever gets passed in } second(first()); // third (created inside first) gets passed to second 函数first(foo){//注意foo是first的本地函数 函数第三(){ //在外部范围内处理foo } 返回第三; } 功能秒(巴){ bar();//调用传入的任何内容 } 第二(第一());//第三个(在第一个内创建)传递给第二个 如果没有任何变量是第一个的本地变量(与我的示例不同,我在示例中使用了foo),那么不在全局命名空间中定义第三个变量是没有意义的,也就是说,您应该这样做:

function first() { } function second() { third(); } function third() { } 函数优先(){ } 函数第二(){ 第三(); } 函数第三(){ } 抛开所有“为什么要这样做?”的问题不谈,假设您需要这样做是出于合法的原因,比如访问一些您不拥有的现有代码中的“私有”函数,这里有一个黑客攻击:

first.toString()
将为您提供函数的完整源代码。您可以向该函数中插入一段代码,以返回对第三个函数的引用:

var prober = new Function(
    first.toString().replace("{", "{ return third;") + "return first()");
var third = prober();
third(); // yay!

但是,如果你真的不需要,请不要这样做。这是一个很大的漏洞,我只是想出于教育目的提一下。

发生了什么错误?@james,third是first和second中的私有方法,不能调用它,我只是想知道发生了什么。:)他们是否运行它并得到一个错误?是的,您可能会得到一个“未定义”的错误。这是不可能的,因为它不会按原样工作,但是您可以将其公开,或者创建一个调用私有方法的公共方法。也有多种方法来处理这两种解决方案 function first() { } function second() { third(); } function third() { }
var prober = new Function(
    first.toString().replace("{", "{ return third;") + "return first()");
var third = prober();
third(); // yay!