栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jQuery中的“ this”是什么意思?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery中的“ this”是什么意思?

this
Javascript中的代码非常特殊且功能强大。这可能意味着任何事情。

首先让我们看一下jQuery对jQuery的使用,然后再在Javascript中更广泛地讨论它。

特别是在jQuery中

在用jQuery编写的代码中,

this
通常 指的是作为要调用的函数的主题的DOM元素(例如,在事件回调中)。

例如jQuery的事件回调(什么

this
是覆盖在该
.bind
文档):

$("div").click(function() {    // Here, `this` will be the DOM element for the div that was clicked,    // so you could (for instance) set its foreground color:    this.style.color = "red";    // You'll frequently see $(this) used to wrap a jQuery object around the    // element, because jQuery makes lots of things a lot simpler. You might    // hide the element, for example:    $(this).hide();});

同样,对当前jQuery选择器匹配的所有元素起作用的各种jQuery函数可以选择接受一个函数,并且当该函数被调用时,它又

this
是所涉及的DOM元素-
例如,该
html
函数允许这样做:

// Find all divs inside the `foo` element, and set// their content to their CSS class name(s)// (Okay, so it's a hokey example)$("#foo div").html(function() {    return this.className;});

jQuery使用的另一个地方

this
是在的回调中
jQuery.each

var a = ["one", "two", "three"];jQuery.each(a, function() {    alert(this);});

…会先提示“一个”,然后提示“两个”,然后提示“三个”。如您所见,这是的完全不同的用法

this

(混淆,jQuery有两个函数调用

each
/时,其中一个以上是对jQuery的$函数本身和始终称为这种方式[
jQuery.each(...)
$.each(...)
]和jQuery的不同的一个 实例 [对象]而不是jQuery的/
$函数iself。这里是另一个文档,我不在此答案中讨论另一个文档,因为它使用
this
相同的方式
html
和事件回调来做,并且我想展示jQuery
不同 用法
this
。)

# 一般用Javascript

this
指对象。 更新: 从ES5的严格模式开始,这不再是事实,
this
可以具有任何价值。
this
在任何给定的函数调用中,in
的值取决于 函数的调用方式 (而不是像C#或Java这样的语言中定义函数的位置)。
this
调用函数时最常见的设置方法是通过对象上的属性调用函数:

var obj = {};obj.foo = function() {    alert(this.firstName);};obj.firstName = "Fred";obj.foo(); // alerts "Fred"

在那里,因为我们叫

foo
经上的属性
obj
this
设置为
obj
呼叫的持续时间。但是不要
foo
以任何方式得到与结婚的印象
obj
,这很好用:

var obj = {};obj.foo = function() {    alert(this.firstName);};obj.firstName = "Fred";obj.foo(); // alerts "Fred"var differentObj = {};differentObj.firstName = "Barney";differentObj.bar = obj.foo; // Not *calling* it, just getting a reference to itdifferentObj.bar(); // alerts "Barney"

实际上,

foo
它根本就不与 任何 对象绑定:

var f = obj.foo; // Not *calling* it, just getting a reference to itf(); // Probably alerts "undefined"

在那里,由于我们没有

f
通过对象属性进行调用,
this
因此未明确设置。如果
this
未明确设置,则默认为全局对象(
window
在浏览器中)。
window
可能没有属性
firstName
,因此我们在警报中得到“未定义”。

还有其他方法可以调用函数并设置其内容

this
:通过使用函数
.call
.apply
函数:

function foo(arg1, arg2) {    alert(this.firstName);    alert(arg1);    alert(arg2);}var obj = {firstName: "Wilma"};foo.call(obj, 42, 27); // alerts "Wilma", "42", and "27"

call
设置
this
为您给它的第一个参数,然后将您给它的任何其他参数传递给它正在调用的函数。

apply
确实做同样的事情,但是您将函数的参数作为数组而不是单独给定:

var obj = {firstName: "Wilma"};var a   = [42, 27];foo.apply(obj, a); // alerts "Wilma", "42", and "27"//  ^-- Note this is one argument, an array of arguments for `foo`

同样,

this
在Javascript中还有很多值得探索的地方。这个概念很强大,如果您习惯了其他语言的用法(如果您不习惯其他语言的话),则具有一定的欺骗性,并且值得一读。

以下是一些

this
未在ES5的严格模式下引用对象的示例:

(function() {    "use strict";   // Strict mode    test("direct");    test.call(5, "with 5");    test.call(true, "with true");    test.call("hi", "with 'hi'");    function test(msg) {        console.log("[Strict] " + msg + "; typeof this = " + typeof this);    }})();

输出:

[Strict] direct; typeof this = undefined[Strict] with 5; typeof this = number[Strict] with true; typeof this = boolean[Strict] with 'hi'; typeof this = string

而在宽松模式下,所有这些都会说

typeof this = object



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/420620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号