javascript 自调用函数javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6715805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:37:42  来源:igfitidea点击:

Self invoking functions javascript

javascript

提问by rubixibuc

I wrote a self invoking function in both firefox and chrome it it wouldn't invoke.

我在 Firefox 和 chrome 中都写了一个自调用函数,它不会调用。

I wrote something to the effect of

我写了一些大意

(function () { alert("THE"); })();

do self invoking functions not work in current browsers?

自调用函数在当前浏览器中不起作用吗?

Thank you

谢谢

edit: I did include all essential tags and all other code works on the page

编辑:我确实在页面上包含了所有基本标签和所有其他代码

回答by ninjagecko

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

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

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

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

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:

因此,由于您的“自调用函数”是 javascript 的基本部分,因此除非内部不工作或您的环境混乱,否则它不可能不起作用。您可以将代码复制粘贴到新的空白页面上,它会正常工作。肯定还有其他问题:

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

检查您的开发控制台中的错误。具体来说,检查以确保您没有语法错误,或者您正在测试的网页没有发生一些奇怪的事情(例如,如果您以某种方式重新定义alert...)。

回答by chim

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

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

回答by Paul

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.

该功能有效。Javascript 支持函数式编程,因此浏览器不要运行该代码,即使对于非常旧的浏览器也是荒谬的。你确定要达成那个声明吗?尝试调试在该语句之前发生的 javascript。

回答by zellio

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

Works in every browser I have installed on this machine.

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

回答by call_de_amberlamps

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.

这个功能绝对有效。我会检查浏览器的控制台是否有页面中的任何 js 错误。也许您可以尝试在脚本的开头放置一个简单的 console.log 函数,以查看是否首先调用了任何 JavaScript。

回答by Gar9276

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.

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

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