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

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

问题描述:

我有3个函数,第一个函数在第二个函数下运行。javascript在另一个函数下运行一个函数

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

function second() { 
    third(); 
} 

我该如何让第二次正确运行第三次?

谢谢。

+0

什么情况发生了错误? – 2009-10-28 20:49:38

+0

@james,第三个是第一个和第二个不能调用的私有方法 – 2009-10-28 20:59:54

+0

我只是想知道发生了什么。 :)他们运行它,并得到一个错误? – 2009-10-28 21:09:03

这取决于您想如何设置第一个功能以及如何访问它。基本上第三种方法是首先是私人的。首先需要一个公共方法来调用第三个方法。该方法将被秒调用。

有多种方法可以做到这一点,我想到的是这样的......

编辑:我命名的参数更好一点,一遍又一遍地这样它不是“参数”。

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'); 
} 

如果你想只是做一个命名空间类的东西,你可以做到这一点(没有私有以及)

// define an object and name it first 
var first = 
{ 
    // give first a public variable but make it a function with an arg 
    third : function(third_param) 
    { 
     alert(third_param); 
    } 

} 

function second() 
{ 
    // call the third method on the first variable 
    first.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!"); 
} 
+0

这太棒了。谢谢。 – Mark 2009-11-03 00:37:39

Javascript不会轻易让它,你为什么需要这样做,你可能想要使用对象或原型来实现这一点。

function first(){ 
    third(); 
} 

function second(){ 

} 

function third(){ 
    second(); 
} 

你为什么不使第三()第一下没有嵌套函数(),然后才有第一()和第二()调用第三() ?

例如

function first(){ 
    third(); 
} 

function second(){ 
    third(); 
} 

function third(){ 

} 

最好的办法就是拉出两者共有的功能,并在需要时调用它。

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 

如果没有那些地方的任何变量先(不像我的例子,我曾经富),有一个在没有确定没有点全球名字空间第三名,即你应该这样做,而不是:

 
function first() { 
} 

function second() { 
    third(); 
} 

function third() { 
} 
+0

有趣的方法 – 2009-10-28 21:12:01

撇开所有“你为什么要这样做?问题,假设你需要有正当的理由比如访问一些现有的代码在一个“私”的功能,你不自己做,这里是一个黑客

first.toString()会给你的整个源功能。你可以注入一些代码到该函数的引用返回到第三个功能:

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

但是,请不这样做如果你真的不需要。这是一个很大的破解,我只是想提到教育目的。

+1

我不会投这个下来,但这是一个非常非常糟糕的主意:) – 2009-10-28 21:34:57