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

时间:2009-10-28 20:46:39

标签: javascript jquery

我有3个函数,第三个函数位于第一个我希望在第二个函数中运行的函数。

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

function second () {
    third ();
}

如何才能正确地进行第二次运行?

感谢。

6 个答案:

答案 0 :(得分:4)

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

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

编辑:我将参数命名为更好一点,这种方式不是一遍又一遍的“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');
}

如果你试图做一个命名空间类的东西,你可以这样做(也没有私有)

// 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!");
}

答案 1 :(得分:0)

Javascript不会轻易地允许它,为什么你首先需要这样做,你可能想要使用对象或原型来完成它。

function first(){
   third();
}

function second(){

}

function third(){
  second();
}

答案 2 :(得分:0)

为什么不让第三个()函数不嵌套在first()下,然后让first()和second()调用third()?

e.g。

function first(){
   third();
}

function second(){
   third();
}

function third(){

}

答案 3 :(得分:0)

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

var thirdf = function third () {
}

function first () {
  thirdf();
}

function second () {
  thirdf();
}

答案 4 :(得分:0)

先获得第三名。如果您的示例看起来更像是这个建议更有意义:

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

如果你没有任何首先是本地的变量(不像我的例子,我使用foo),没有必要在全局命名空间中定义第三个,即你应该这样做:

function first() {
}

function second() {
  third();
}

function third() {
}

答案 5 :(得分:0)

暂且不说“你为什么要这样做?”问题,并假设您出于合理的原因需要这样做,例如在您不拥有的某些现有代码中访问“私有”功能,这里是一个黑客

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

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

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

相关问题