問題:
I was told today that it's possible to invoke a function without parentheses.今天有人告訴我,可以調用不帶括號的函數。 The only ways I could think of was using functions like apply
or call
.我能想到的唯一方法是使用諸如apply
或call
類的函數。
f.apply(this);
f.call(this);
But these require parentheses on apply
and call
leaving us at square one.但是這些需要括號中的apply
和call
讓我們在第一個地方離開。 I also considered the idea of passing the function to some sort of event handler such as setTimeout
:我還考慮了將函數傳遞給某種事件處理程序(例如setTimeout
的想法:
setTimeout(f, 500);
But then the question becomes "how do you invoke setTimeout
without parentheses?"但是接下來的問題就變成了“如何在沒有括號的情況下調用setTimeout
?”
So what's the solution to this riddle?那麼這個謎語的解法是什麼? How can you invoke a function in Javascript without using parentheses?如何在不使用括號的情況下調用 Javascript 中的函數?
解決方案:
參考一: https://stackoom.com/question/2Qq7W參考二: Invoking a function without parentheses