自调用函数 javascript
我在 Firefox 和 Chrome 中编写了一个自调用函数,但它不会调用。
我写了一些东西,大意是
(function () { alert("THE"); })();
自调用函数在当前浏览器中不起作用吗?
我确实包含了页面上所有必要的标签和所有其他代码
I wrote a self invoking function in both firefox and chrome it it wouldn't invoke.
I wrote something to the effect of
(function () { alert("THE"); })();
do self invoking functions not work in current browsers?
I did include all essential tags and all other code works on the page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
“自调用函数”实际上并不是 javascript 的一部分,它只是人们调用特定代码模式(如 AJAX 等)的术语;这些模式应该在 javascript 工作的任何地方工作。
您所说的“自调用函数”只是创建一个匿名函数并立即调用它(而不是将其存储在 var 中,作为对象值、函数参数等)。
也就是说,以下内容基本上是相同的:
因此
,因为您的“自调用函数”是 javascript 的基本部分,所以除非内部不工作或您的环境混乱,否则它不可能不工作。您可以将代码复制粘贴到新的空白页上,这样就可以正常工作。肯定还有其他问题:
在开发控制台中检查错误。具体来说,检查以确保您没有语法错误,或者您正在测试的网页没有发生一些奇怪的事情(例如,如果您以某种方式重新定义
alert
.. .)。"Self-invoking functions" are not really a part of javascript, it's just a term that people are calling a specific pattern of code (like AJAX, etc.); these patterns should work anywhere that javascript works.
What you're calling a "self-invoking function" is just creating an anonymous function and immediately calling it (as opposed to say storing it in a var, as an object value, as a function param, etc.).
That is, the following are basically the same:
and
So because your 'self-invoking function' is a basic part of javascript, there is no possible way it's not working unless the insides aren't working or your environment is messed up. You could copy-paste your code onto a new blank page, and it would work fine. Something else must be going wrong:
Check your errors in your dev console. Specifically, check to make sure you don't have a syntax error or that there isn't some weird thing going on with the webpage you're testing it on (e.g. if you somehow redefine
alert
...).我遇到了一个自调用函数的问题,它产生了这个错误...
问题是由于在左括号之前没有分号结束该行引起的
I had this issue with a self invoking function which produced this error...
The problem was caused by not having a semi colon ending the line before the opening bracket
适用于我在这台机器上安装的每个浏览器。
Works in every browser I have installed on this machine.
该功能有效。 Javascript 支持函数式编程,因此对于浏览器来说,不运行该代码,即使对于非常旧的浏览器来说也是荒谬的。您确定正在达成该声明吗?尝试调试该语句之前发生的 javascript。
That function works. Javascript supports functional programming, so for a browser not to run that code, even for a very old browser that would be absurd. Are you sure that statement is being reached? Try debugging javascript that occurs before that statement.
这个功能绝对有效。我会检查您的浏览器控制台是否有页面中的任何 js 错误。也许您可以尝试在脚本的开头放置一个简单的 console.log 函数,以查看是否首先调用了任何 JavaScript。
This function definitely works. I would check your browser's console for any js errors in your page. Perhaps you could try to put a simple console.log function at the beginning of your script to see if any JavaScript is being called in the first place.
这个带有返回值的自调用函数可以在所有当前浏览器(Safari、Chrome 和 Firefox)中正常工作。该函数立即、自动、匿名执行。
This self invoking function with return value will work in all current browsers(Safari, Chrome and Firefox) without issue. This function executes immediately, automatically and anonymously.
我有类似的问题。我在下面提到它。
我无法在任何浏览器上运行这个自调用函数
,但每当我添加如下所示的 window.onload 时,它都工作正常:
I had a similar problem. I'm mentioning it below.
I couldn't run this self-invoking function on any browser
but whenever I added window.onload like below, it worked fine: