问题:
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