Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我发现了一种定义函数的有趣方法:
! function() { function myFunction() { return returnValue; } }();
但是,这个功能不能直接从浏览器控制台调用,怎么实现呢?
那是一个围绕您的函数的 IIFE(立即调用函数表达式)。
我建议对您编写的代码使用这种方法:
!function() { function myFunction() { return 'hello'; } window['myFunction'] = myFunction; }();
现在myFunction在控制台中调用。以前myFunction隐藏在您的 IIFE 中,并且没有作为全局变量公开。
myFunction