我有這樣的:運行函數中的函數
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.
}
你能解釋你爲什麼*不想做你描述的嵌套定義/調用嗎?這是顯而易見的解決方案,所以如果你解釋爲什麼它不適合你,它將幫助其他人創造更好的答案。 – maerics 2012-01-13 16:14:58
你能詳細說一下嗎?什麼是需要你的嵌套功能的條件? alsocool是一個很酷的本地函數,不能在其外部調用。將alsocool移出,或者在其內部調用它。那些是我看到的唯一選擇。你有鏈接或完整的樣本來說明你的約束嗎? – 2012-01-13 16:18:04
對不起,不清楚。我編輯了我的問題。 – jQuerybeast 2012-01-13 16:25:41