自调用函数 javascript

发布于 2024-11-24 17:54:52 字数 182 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风筝有风,海豚有海 2024-12-01 17:54:52

“自调用函数”实际上并不是 javascript 的一部分,它只是人们调用特定代码模式(如 AJAX 等)的术语;这些模式应该在 javascript 工作的任何地方工作。

您所说的“自调用函数”只是创建一个匿名函数并立即调用它(而不是将其存储在 var 中,作为对象值、函数参数等)。

也就是说,以下内容基本上是相同的:

var f = function(){...}; f()

因此

( function(){...} )()

,因为您的“自调用函数”是 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:

var f = function(){...}; f()

and

( function(){...} )()

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...).

落在眉间の轻吻 2024-12-01 17:54:52

我遇到了一个自调用函数的问题,它产生了这个错误...

Uncaught TypeError: object is not a function

问题是由于在左括号之前没有分号结束该行引起的

I had this issue with a self invoking function which produced this error...

Uncaught TypeError: object is not a function

The problem was caused by not having a semi colon ending the line before the opening bracket

寂寞陪衬 2024-12-01 17:54:52
<script type="text/javascript">
  (function() {
     alert('Hello World!');
  })();
</script>

适用于我在这台机器上安装的每个浏览器。

<script type="text/javascript">
  (function() {
     alert('Hello World!');
  })();
</script>

Works in every browser I have installed on this machine.

友谊不毕业 2024-12-01 17:54:52

该功能有效。 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.

伴我心暖 2024-12-01 17:54:52

这个功能绝对有效。我会检查您的浏览器控制台是否有页面中的任何 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.

心不设防 2024-12-01 17:54:52

这个带有返回值的自调用函数可以在所有当前浏览器(Safari、Chrome 和 Firefox)中正常工作。该函数立即、自动、匿名执行。

<script type="text/javascript">
    alert((function(){
        return("Hello World");
    })());
</script>

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.

<script type="text/javascript">
    alert((function(){
        return("Hello World");
    })());
</script>
哀由 2024-12-01 17:54:52

我有类似的问题。我在下面提到它。

我无法在任何浏览器上运行这个自调用函数

(function myfunc() {
    var x = 34;
    console.log(x);
})();

,但每当我添加如下所示的 window.onload 时,它都工作正常:

window.onload = (function myfunc() {
    var x = 34;
    console.log(x);
})();

I had a similar problem. I'm mentioning it below.

I couldn't run this self-invoking function on any browser

(function myfunc() {
    var x = 34;
    console.log(x);
})();

but whenever I added window.onload like below, it worked fine:

window.onload = (function myfunc() {
    var x = 34;
    console.log(x);
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文