好吧,這是很難解釋的,但我在這裏演示了我正在嘗試完成的東西。在一個函數中傳遞一個參數在另一個函數中被「參考」傳遞
我認爲這與How can I pre-set arguments in JavaScript function call? (Partial Function Application)有關,但我無法弄清楚如何將這篇文章中提到的方法應用到我的場景中。
我有三個功能。
- func1。一個函數傳遞給func2並被其調用一次 func2完成其業務。
- func2。將要調用的函數(func1) 作爲參數接收的函數。
- func3。調用func2的函數。
我想傳遞一個參數給func3,它傳遞給func2中的func1。
例:
<input type='button' id='testButton' onclick='func3()' value='Click Me' />
<input type='button' id='testButton2' onclick='func3a(true)' value='Click Me Also' />
func3 = function (aBool) {
func2(func1);
}
// I want to be able to pass a parameter to the function
// that gets called in func2
func3a = function (aBool) {
func2(func1(aBool));
}
func1 = function (data, myBool) {
alert(data[0] + ' ' + myBool);
}
// Cannot Change this function
func2 = function (func) {
var data = [];
data[0] = "Hello World"
func(data);
}
謝謝,這在我的例子和實際代碼中都有效。我最近在YouTube上觀看了Crockford的一些視頻,而且我知道我曾經在某處看到過這個視頻。只是沒有把我的手指。 – Popo