2009-10-28 98 views
2

我有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

回答

4

這取決於您想如何設置第一個功能以及如何訪問它。基本上第三種方法是首先是私人的。首先需要一個公共方法來調用第三個方法。該方法將被秒調用。

有多種方法可以做到這一點,我想到的是這樣的......

編輯:我命名的參數更好一點,一遍又一遍地這樣它不是「參數」。

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

0

Javascript不會輕易讓它,你爲什麼需要這樣做,你可能想要使用對象或原型來實現這一點。

function first(){ 
    third(); 
} 

function second(){ 

} 

function third(){ 
    second(); 
} 
0

你爲什麼不使第三()第一下沒有嵌套函數(),然後纔有第一()和第二()調用第三() ?

例如

function first(){ 
    third(); 
} 

function second(){ 
    third(); 
} 

function third(){ 

} 
0

最好的辦法就是拉出兩者共有的功能,並在需要時調用它。

var thirdf = function third() { 
} 

function first() { 
    thirdf(); 
} 

function second() { 
    thirdf(); 
} 
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 

如果沒有那些地方的任何變量先(不像我的例子,我曾經富),有一個在沒有確定沒有點全球名字空間第三名,即你應該這樣做,而不是:

 
function first() { 
} 

function second() { 
    third(); 
} 

function third() { 
} 
+0

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

0

撇開所有「你爲什麼要這樣做?問題,假設你需要有正當的理由比如訪問一些現有的代碼在一個「私」的功能,你不自己做,這裏是一個黑客

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

相關問題