这是什么语法?;(function ($, undefined)
What is this syntax? ; (function ($, undefined)
; (function ($, undefined)
{
// all the variables and functions of the js document
})(jQuery);
我已经在jquery/javascript文件中看到过两次缩放脚本。我不明白这到底是什么。我不记得最近在学习jquery和js时在tizag或w3schools上遇到过这个。
这段代码前后没有任何内容(除了一些注释)。所以我完全不知道(function())(jQuery);
是什么或者做什么。
(function ($, undefined)
{
// all the variables and functions of the js document
})(jQuery);
调用一个代码块,确保在
内部- $可用于引用
jQuery
-
undefined
是undefined
(编辑:这是有用的,因为undefined
可以在最老的浏览器重新定义,它现在是无用的)
,并且任何minifier都可以将undefined
更改为更短的标签。
初始化的;
确保您可以将这个文件与另一个文件连接起来:如果没有这个,如果前面的文件类似于
(function (){
})()
这是一种确保$
确实是jQuery
对象的方法,并确保任何局部变量和方法都是私有作用域,即不污染全局命名空间。
这是一个自调用的匿名函数,传递的参数是jQuery,这意味着$
将是jQuery
对象。
在函数内部声明意味着内部变量和方法在函数外部是不可见的。
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 直接在函数声明上使用function.prototype.bind
- jQuery:.click(function(){(element),collapse('show',f
- /undefined在我的404错误日志中多次出现
- $scope.$watch(var,function) undefined 不是函数
- jQuery undefined after $(function() {
- jQuery .forEach() 导致错误,“undefined is not a function”
- JavaScript中“(function(window, undefined){})(window)”是什么意思?
- 使用“flickrnode”API时出现“TypeError: undefined is not a function”
- "undefined is not a function" onSuccessHandler
- 莫名其妙的“Undefined is not a function”错误 — 由“getElementsById”引起
- jQuery/JavaScript Undefined Function Error
- SoundCloud SDK Play Function - trackId Undefined
- JQuery on() undefined function
- undefined is not a function when evaluating 'this.batman
- “uncatch TypeError: undefined is not a function” 在使用 str.ind
- 谷歌地图API(使用gmapsjs), setCenter抛出“undefined is not a function”
- 这是什么语法?;(function ($, undefined)
- function .prototype.apply给我留下undefined不是一个函数错误
- 在骨干应用视图中出现" Uncaught TypeError: undefined is not a function