jquery/JavaScript quickie,那些括号里有什么
jquery/javascript quickie, what are those parenthesis holding?
我遇到了一些javascript或jQuery函数,它们在方法的末尾有一个封闭的值或对象。 例:
(function ($) {
var delay = 0;
$.fn.translate3d = function (translations, speed, easing, complete) {
var opt = $.speed(speed, easing, complete);
opt.easing = opt.easing || 'ease';
translations = $.extend({ x: 0, y: 0, z: 0 }, translations);
return this.each(function () {
var $this = $(this);
$this.css({
transitionDuration: opt.duration + 'ms',
transitionTimingFunction: opt.easing,
transform: 'translate3d(' + translations.x + 'px, ' + translations.y + 'px, ' + translations.z + 'px)'
});
setTimeout(function () {
$this.css({
transitionDuration: '0s',
transitionTimingFunction: 'ease'
});
opt.complete();
}, opt.duration + (delay || 0));
});
};
})(jQuery);
或
<script type="text/javascript">
(function (d, t) {
<snip>
})(document, 'script');
</script>
函数末尾那些用括号括起来的项目的目的是什么?我在SO上找到了几个答案,但没有一个可以清除它。 谢谢
它使用参数定义一个匿名函数,将值分配给函数的参数,然后调用它。
此方法的优点是,您不会用函数和变量污染命名空间,而这些函数和变量不会在函数内以外的其他任何地方使用。
更详细地说,
(function() {} ...
声明匿名函数,并在末尾添加()
调用刚刚创建的函数。
它形成一个自调用匿名函数。优点是所有标识符的范围都限定在该函数内,而不是具有全局范围。
这是一个很好的链接,可以了解更多细节:http://markdalgleish.com/2011/03/self-executing-anonymous-functions/
下面是一个示例:
(function() {
var foo = "bar";
console.log(foo); // prints bar
})();
console.log(foo); // prints undefined since foo is scoped within the function
当像这样重写时,可能更容易理解上面的代码片段:
function fun() {
var foo = "bar";
console.log(foo);
}
fun();
这两个代码片段实现相同的目标。唯一的区别是,在第一个代码段中,您没有命名函数然后调用它。而是将其创建为匿名函数,并立即调用它。
当您想要将对象绑定到某个变量时,这也很有用,例如在代码片段中使用jQuery
完成:
var $ = {}; // define $ to an empty object instead of the jQuery object
(function($) {
// within this function, $ will always be the jQuery object
// protecting you from re-definitions outside the funciton
console.log($);
})(jQuery);
上面的代码片段创建了一个匿名函数,该函数接受单个参数 $
,然后立即调用传入 jQuery
对象的函数,确保在函数中$
始终引用jQuery
对象。
自调用匿名函数的另一个有趣的应用是,当您想要在延迟执行的情况下将变量绑定到特定对象时。
var markers = []; //an array of google map markers
for(var i = 0; i < markers.length; i++) {
// do something with markers[i];
// then schedule it for removal after 2 seconds
setTimeout(function() { markers[i].setMap(null); }, 2000);
}
上述代码片段的问题在于,当标记删除代码在 2 秒后运行时,i
的值届时会发生变化,因为循环会继续。这可以通过使用自调用 anon 函数创建闭包来补救:
for(var i = 0; i < markers.length; i++) {
// do something with markers[i];
// then schedule it for removal after 2 seconds
(function(j) {
setTimeout(function() { markers[j].setMap(null); }, 2000);
})(i);
}
现在,j
绑定到当前值 i
,因此当删除代码在 2 秒后运行时,它会以预期的值 i
运行,即使此后i
已更改。
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 全局变量和全局对象的属性之间有什么区别吗
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 在ember/handlers中使用value和valueBinding有什么区别
- 什么'在webpack外部设置中,reactDom和reactDom不同
- 在JavaScript中拆分日期字符串的更好方法是什么
- 为什么$.brower被弃用?还有什么更好的替代方案
- 什么是&&在没有if的行中的变量之间
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- 处理浮点错误的最佳方法是什么
- DOM元素和angular元素之间的主要区别是什么
- javascript导入的最佳实践是什么
- JavaScript意外的令牌<,有什么方法可以逃避错误(或至少跳过那些无用的字符)
- jquery/JavaScript quickie,那些括号里有什么
- 在那些显然不支持此代码的浏览器中,有什么方法可以达到这种效果吗
- 那些自动执行的匿名函数(又名IIFE)实现有什么区别?