[英]var $ = this.jQuery cannot understand
(function () {
var $ = this.jQuery;
this.myExample = function () {};
}());
Can you tell me what this line means: 你能告诉我这条线是什么意思吗:
var $ = this.jQuery;
I am new to javascript 我是javascript的新手
since it is an IIFE , this
refers to the window object inside the scope of the function(since it may not be using strict mode), so we are creating a local variable called $
which refers to the global jQuery variable for easy access. 因为它是一个IIFE ,
this
是指函数范围内的窗口对象(因为它可能不使用严格模式),所以我们创建一个名为$
的局部变量,它引用全局jQuery变量以便于访问。
But it can be simplified as 但它可以简化为
(function ($) {
this.myExample = function () {};
}(jQuery));
var $ = this.jQuery;
means $
as this.jQuery;
意思是
$
as this.jQuery;
in (function () {}());
在
(function () {}());
scope. 范围。
It is a variable, you can say that $
will have the value of this.jQuery
. 它是一个变量,你可以说
$
将具有this.jQuery
的值。
var $ = this.jQuery;
var is a short for variable, $ is the name of that variable, and the value of it is on the right side. var是变量的缩写,$是该变量的名称,它的值在右侧。
That line is is mapping $ to this.jQuery. 那一行是将$映射到this.jQuery。 Essentially so when calling a function that uses this.jQuery object, $ can be used instead., for example you would use $.somefunction() instead of this.jQuery.somefunction()
基本上当调用使用this.jQuery对象的函数时,可以使用$代替。例如,你可以使用$ .somefunction()而不是this.jQuery.somefunction()
The self executing function that encloses those two lines creates the scope. 包含这两行的自执行函数创建范围。 "this" would be the document.
“这”将是文件。 If jQuery is loaded then there is this.jQuery so $ becomes this documents jQuery.
如果加载了jQuery,那么就有this.jQuery,所以$成为了这个文档的jQuery。
In JavaScript, every function is invoked on an object that can be accessed with this
. 在JavaScript中,每个函数被调用,可以与被访问的对象在
this
。 What this
is depends on the way the function is called, and defaults to window
: 什么
this
是默认值取决于函数的调用方式,和window
:
obj1.func(); // "this" inside "func" will be "obj1"
func.call(obj2); // "this" inside "func" will be "obj2"
func(); // "this" will default to "window"
In your case, you are using the third way: (function(){...})()
calls the function "unbound", and this
will default to the global window
object. 在您的情况下,您正在使用第三种方式:
(function(){...})()
调用函数“unbound”, this
将默认为全局window
对象。
var $ = this.jQuery;
Will now bind the value of window.jQuery
to the variable name $
, which happens to be a valid identifier name in JavaScript. 现在将
window.jQuery
的值绑定到变量名$
,这恰好是JavaScript中的有效标识符名。 As the jQuery library provides all its functionality in a global function window.jQuery
, this is a convenient way to access it using the shorter name $
: 由于jQuery库在全局函数
window.jQuery
提供了它的所有功能,因此这是使用较短名称$
访问它的便捷方式:
$("a").click();
To experiment with this
in JavaScript, try out the following in your browser's JavaScript console: 要在JavaScript中进行
this
试验,请在浏览器的JavaScript控制台中尝试以下操作:
function alertThis() { alert(this); }
var obj = {method: alertThis};
alertThis(); // will show something similar to [object Window], as "this" is "window"
alertThis.call("Foobar") // will show "Foobar"
obj.method(); // will show something similar to [object Object], as "this" is "obj"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.