Javascript 如果你读了原始问题,OP指定了3种编写闭包的方法,他将这些方法命名为类型{1,2,3},我指的是问题中的区域,并给出了答案..(function(){})意味着返回内部函数?“()”的确切含义是什么?我喜欢第一个例子,但第二个例子使用行号引用(这不是js功
Javascript 如果你读了原始问题,OP指定了3种编写闭包的方法,他将这些方法命名为类型{1,2,3},我指的是问题中的区域,并给出了答案..(function(){})意味着返回内部函数?“()”的确切含义是什么?我喜欢第一个例子,但第二个例子使用行号引用(这不是js功,javascript,jquery,Javascript,Jquery,如果你读了原始问题,OP指定了3种编写闭包的方法,他将这些方法命名为类型{1,2,3},我指的是问题中的区域,并给出了答案..(function(){})意味着返回内部函数?“()”的确切含义是什么?我喜欢第一个例子,但第二个例子使用行号引用(这不是js功能)并不清楚。这叫做立即调用函数表达式(IIFE):我在理解(Function(){})的外圆括号时遇到了很多问题:这到底是什么?我不能只写函数(){}()?对我来说,这是最好的答案。清晰而简单 (function($) { })(jQuer
如果你读了原始问题,OP指定了3种编写闭包的方法,他将这些方法命名为类型{1,2,3},我指的是问题中的区域,并给出了答案..(function(){})意味着返回内部函数?“()”的确切含义是什么?我喜欢第一个例子,但第二个例子使用行号引用(这不是js功能)并不清楚。这叫做立即调用函数表达式(IIFE):我在理解
(Function(){})
的外圆括号时遇到了很多问题:这到底是什么?我不能只写函数(){}()
?对我来说,这是最好的答案。清晰而简单
(function($) {
})(jQuery);
(function($) {
$.fn.jPluginName = {
},
$.fn.jPluginName.defaults = {
}
})(jQuery);
(function($) {
$.jPluginName = {
}
})(jQuery);
(function($){
//Attach this new method to jQuery
$.fn.extend({
var defaults = {
}
var options = $.extend(defaults, options);
//This is where you write your plugin's name
pluginname: function() {
//Iterate over the current set of matched elements
return this.each(function() {
//code to be inserted here
});
}
});
})(jQuery);
1. (
2. function(){}
3. )
4. ()
1. function(){ .. }
2. (1)
3. 2()
(function(doc){
doc.location = '/';
})(document);//This is passed into the function above
(function($){
//Attach this new method to jQuery
$.fn.extend({
//This is where you write your plugin's name
'pluginname': function(_options) {
// Put defaults inline, no need for another variable...
var options = $.extend({
'defaults': "go here..."
}, _options);
//Iterate over the current set of matched elements
return this.each(function() {
//code to be inserted here
});
}
});
})(jQuery);
(function($) {
...
})(jQuery);
(function($) {
// do something
})(jQuery);
// Clousure declaration (aka anonymous function)
var f = function(x) { return x*x; };
// And use of it
console.log( f(2) ); // Gives: 4
// An inline version (immediately invoked)
console.log( (function(x) { return x*x; })(2) ); // Gives: 4
var $f = function($) { return $*$; };
var jQuery = 2;
console.log( $f(jQuery) ); // Gives: 4
// An inline version (immediately invoked)
console.log( (function($) { return $*$; })(jQuery) ); // Gives: 4