2012-01-13 70 views
0

我有這樣的:運行函數中的函數

function cool(){ 
    function alsocool(){ 

    } 
} 

和我按一下按鈕運行涼():

$(selector).on('click', function(){ 
cool(); 
} 

如何運行的cool()alsocool()從同樣的點擊?請注意,我想做的事:

function cool(){ 
    function alsocool(){ 

    } 
    alsocool(); 
} 

如果我做的:

$(selector).on('click', function(){ 
    cool(); alsocool(); 
    } 

這是行不通的。

是否有可能在同一個調用中的函數內部運行函數?

編輯:

我確實想通過cool()因爲顯然alsocool()沒有一次其內部功能cool()cool();公認是從許多選擇過去了因此,我想知道從哪個選擇是通過採取適當的行動。

例子我想是這樣的:

function cool(){ 

// If this was called by button1, run alsocool() else bypass it 
     function alsocool(){ 

     } 
// some code goes here 

} 

$("#button1").on('click', function(){ 
     cool(); alsocool(); 
     // If button1 clicked, run cool AND alsocool 
     } 
$("#button2").on('click', function(){ 
     cool(); // If button2 clicked, run cool ONLY. 
    } 
+1

你能解釋你爲什麼*不想做你描述的嵌套定義/調用嗎?這是顯而易見的解決方案,所以如果你解釋爲什麼它不適合你,它將幫助其他人創造更好的答案。 – maerics 2012-01-13 16:14:58

+1

你能詳細說一下嗎?什麼是需要你的嵌套功能的條件? alsocool是一個很酷的本地函數,不能在其外部調用。將alsocool移出,或者在其內部調用它。那些是我看到的唯一選擇。你有鏈接或完整的樣本來說明你的約束嗎? – 2012-01-13 16:18:04

+0

對不起,不清楚。我編輯了我的問題。 – jQuerybeast 2012-01-13 16:25:41

回答

2

答案很簡單:這是不可能的。
內部函數對於包含函數的作用域是局部的,所以除非該函數調用它,否則根本不能調用它。

如果您希望可以從外部訪問這兩種功能,請在cool之外定義alsocool,即與cool的級別相同。


根據您的評論,下面是一個會用一個參數來確定內部函數應該被稱爲與否的方式:

function cool(callInner){ 
    function alsocool(){ 

    } 
    if(callInner) { 
     alsocool(); 
    } 
} 
+0

真的嗎?沒有任何hacky的方式來傳遞一個變量:cool(abc);然後在函數cool()內部,如果abc通過,請執行alsocool()? – jQuerybeast 2012-01-13 16:15:44

+1

這當然是可能的,但你明確表示你不想在函數內部調用它! – ThiefMaster 2012-01-13 16:16:21

+0

正是我在找的東西。對不起,不清楚。我編輯了我的問題。 – jQuerybeast 2012-01-13 16:22:43

1

的問題是,因爲你定義的函數alsocoolcool之內,它的可見度僅限於該範圍。

因此,您可以只有cool調用函數alsocool

你可以,當然,移動alsocoolcool的聲明,這將仍然允許您呼叫alsocoolcool內,但你將從alsocool鬆訪問的cool範圍。

根據傳遞的參數,您也可以限制alsocoolcool之內的調用,如果這是您可行的選項;

function cool(alsoAlsoCool){ 
    function alsocool(){ 

    } 

    if (alsoAlsoCool) { 
     alsocool(); 
    } 
} 

// cool(true) will call it, but cool() or cool(false) won't. 
+0

謝謝。這正是我所期待的。 @ThieftMaster回答更快,因此我會接受他的回答。謝謝你。 – jQuerybeast 2012-01-13 16:24:40

2

如果你

function cool() { 
    function alsocool() { ... } 
} 

然後 'alsocool',而酷()函數執行時存在。它不會從外部訪問。

你會想:

function cool() { ... } 
function alsocool() { ... } 

$(selector).click(function() { 
    cool(); 
    alsocool(); 
}): 
+0

請參閱我的編輯 – jQuerybeast 2012-01-13 16:22:12

+0

感謝您的回答並對不起,我首先不清楚 – jQuerybeast 2012-01-13 16:28:06

1

你不能做到這一點。 alsocool只存在於cool內,點擊處理程序不知道alsocool存在。

如果你不想從cool內部撥打alsocool,那麼你將不得不使alsocool全球。

+0

我明白,因此我先傳遞cool(),然後再通過alsocool()方法傳遞。例如:IF selector1單擊,傳遞cool()然後alsocool()。如果selector2點擊,只傳遞cool()?不是有可能嗎? – jQuerybeast 2012-01-13 16:17:24

+0

@Jonathan:你可以讓'cool'返回'alsocool',然後調用它需要的。你也可以將元素傳遞給'cool',並讓它確定是否調用'alsocool'或不。 – 2012-01-13 16:19:34

+0

抱歉不清楚。我編輯了我的問題。什麼小偷大師回答我想它是我正在尋找的。謝謝 – jQuerybeast 2012-01-13 16:23:30

1

我不明白你爲什麼要這麼做,但你可以這樣做:

function cool() 
{ 
    arguments.callee.alsoCool = function() { 
     alert("also cool"); 
    }; 

    alert("cool"); 
} 

$("#b").click(function() { 
    cool(); 
    cool.alsoCool(); 
}); 

現場演示:http://jsfiddle.net/ENqsZ/

另外,作爲火箭隊的建議,你可以這樣做:

function cool() 
{ 
    alert("cool"); 

    return function() { 
     alert("also cool"); 
    }; 
} 

$("#b").click(function() { 
    var alsoCool = cool(); 

    alsoCool(); 
}); 
+1

我建議返回一個函數,而不是使用'arguments.callee'。 – 2012-01-13 16:20:32

+0

我認爲它不是Rocket建議的。請參閱ThieftMaster的回答 – jQuerybeast 2012-01-13 16:29:09