什么!Javascript中的函数是什么意思? [英] what does !function in Javascript mean?
问题描述
很抱歉发布这个但是!功能不是google-able而我在我的JavaScript代码中找不到它。
Sorry for posting this but !function is not google-able and I did not find it in my JavaScript code.
以下是Twitter使用它的方式:
Here is how Twitter uses it:
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://google.com" data-text="Google says">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
来自 https://twitter.com/about/resources/buttons#
推荐答案
这是简写或替代自我调用匿名函数:
(function(){
// code
})();
可写:
!function(){
// code
}();
您还可以使用 +
代替!
。
如果您只是这样做:
function(){
// code
}();
这会产生问题,这就是你需要添加的原因!
之前将 函数声明 转换为 函数表达式 。
That will create problems, that's why you need to add !
before it which turns the function declaration into function expression.
ExpressionStatement不能以函数关键字开头,因为
可能使其与FunctionDeclaration不一致。
An ExpressionStatement cannot start with the function keyword because that might make it ambiguous with a FunctionDeclaration.
为了更好地理解这个概念,你应该看看:
To better understand the concept, you should check out:
- Function Declarations vs. Function Expressions
这篇关于什么!Javascript中的函数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!