我没有找到一个要求类似于这个模式的帖子,抱歉,如果我错过了它.
无论如何,我在许多jQuery插件或脚本中重复看到这种模式:
(function () {
...........
}());
Run Code Online (Sandbox Code Playgroud)
它的目的是什么?它有名字吗?
谢谢!
我有字符串包含anonymus函数定义,但我怎么称呼它.让我们说功能是这样的:
var fn_str = "function(){ alert('called'); }";
Run Code Online (Sandbox Code Playgroud)
试过eval,但得到一个错误,该函数必须有一个名字.
eval(fn_str).apply(this); // SyntaxError: function statement requires a name
Run Code Online (Sandbox Code Playgroud) 这是我的问题.我有一个数组,其中包含我需要查找天气的城市名称.所以我循环遍历每个城市并执行AJAX请求以检索天气.
var LOCATION = 'http://www.google.com/ig/api?weather=';
$( document ).ready( function() {
for( var cityIdx = 0; cityIdx < cities.length; cityIdx++ ) {
$.ajax({
type: 'GET',
url: LOCATION + cities[ cityIdx ],
dataType: 'xml',
success: function( xml ) {
if( $( xml ).find( 'problem_cause' ) != 0 ) {
// Do what I want with the data returned
var weather = $( xml ).find( 'temp_c' ).attr( 'data' );
}
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在成功功能中,我无法访问城市名称(通过城市[cityIdx]).我在for循环和成功函数中插入了一个alert(),似乎循环执行cities.length次,然后我得到成功函数警报.我的目标是简单地遍历每个城市,获取天气并在我的页面上显示它以及相关的城市名称.
另外,您建议我将内容与演示文稿分开?
谢谢.:)
我已经使用javascript一段时间了,我开始意识到我使用该语言有多糟糕.它的一些更高级的方面是关闭,如闭合,模块模式等.
有人可以解释()操作符在放置在函数末尾时是如何工作的,也就是为什么它会立即调用它 - 我以前没有看过这个并且对这实际上是如何工作感到困惑.
如:
var myFunct = function() {
alert("here");
}();
Run Code Online (Sandbox Code Playgroud)
最后的()将立即调用该函数.
可能重复:
以下JavaScript构造是否称为Closure?
围绕JavaScript对象/函数/类声明的括号是什么意思?
(function(){})()构造如何工作以及人们为什么使用它?
(function(){})()之间的区别; 和function(){}();
嗨,
因为我正在查看一个只有简单的html文本的站点而没有图形我想到查看它的代码.
当我这样做时,我找到了这个脚本
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-13137273-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Run Code Online (Sandbox Code Playgroud)
使用如下代码有什么特殊目的
(function() { /*code;*/ })();
Run Code Online (Sandbox Code Playgroud)
我在jquery中看到过它.但我对自由风格的极端JavaScript编程仍然不太满意.
建议,意见和答案.任何都会很好.
谢谢
[我在第一个答案后写这个.在某种意义上的极端......这是我在其他编程语言中找不到的,而且我无法猜到它可能是什么.除了javascript中有很多非凡的选项,我提到它是极端的.也许我应该使用另一项工作.]
在声明函数(最后一行)后,括号正在做什么?
var a = 0;
function foo(a) {
alert(a);
} (a);
Run Code Online (Sandbox Code Playgroud) 代码1:
var x=(function(){
return {
greet:function(){
alert('Hello from code1');
}
};
})();
Run Code Online (Sandbox Code Playgroud)
代码2:
var x=function(){
return {
greet:function(){
alert('Hello from code2');
}
};
}();
Run Code Online (Sandbox Code Playgroud)
两者都将被调用为:
x.greet();
Run Code Online (Sandbox Code Playgroud)
在样式1中,我在括号内包含了自执行函数,而在第二个代码中却没有.两者都一样.那么code1和code2之间的区别是什么呢?
我在网站上找到了以下代码.
var testModule = (function(){
var counter = 0;
return {
incrementCounter: function() {
return counter++;
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
})();
Run Code Online (Sandbox Code Playgroud)
所以它遵循语法 var a = (blah balh..)()
它究竟意味着什么?变量声明的含义是什么a =()()...
可能重复:
围绕JavaScript对象/函数/类声明的括号是什么意思?
什么(function($){})(jQuery); 意思?
我试图理解Edge是如何工作的,所以我可以使用自己的代码,
我之前没有遇到这个问题,但这意味着什么:
(function(symbolName) {
//CODE
})("stage");
Run Code Online (Sandbox Code Playgroud)