在Javascript中,此语法是什么意思? [英] In Javascript, what does this syntax mean?

查看:128
本文介绍了在Javascript中,此语法是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看着jQuery插件时发现了这个片段,想知道它的实际作用

I found this snippet when i was looking at jQuery plugins and wonder what it actually does

一个jQuery插件框架:

A jQuery plugin skeleton:

(function($) {
    ... 
})(jQuery); 

最近在

推荐答案

这将创建一个匿名函数并直接调用它: 等同于

This creates a anonymous function and calls it directly: this is equivalent to

var fun = function(){};
fun();

它在jquery插件中用于确保与定义全局变量'$'的其他库的兼容性.在您的插件框架中,您将插件包装在一个匿名函数中,该函数接收一个名为"$"的参数(因此将覆盖全局变量"$"),然后将该匿名函数以"jQuery"作为参数进行调用,因此有效地$ = jQuery,但仅限于该匿名函数内.

its used in jquery plugins to ensure compatibility with other libraries defining a global variable '$'. in your plugin sekeleton, you wrap your plugin in a anonymous function, which receives an argument named '$' (thus overriding a global variable '$'), this anonymous function is then called with 'jQuery' as parameter, so effectively $ becomes = jQuery, but only within that anonymous function.

这篇关于在Javascript中,此语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆