Home  >  Article  >  Web Front-end  >  How does \"this\" behave differently in jQuery and regular JavaScript, and what are the implications for working with DOM elements and function calls?

How does \"this\" behave differently in jQuery and regular JavaScript, and what are the implications for working with DOM elements and function calls?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 08:18:02838browse

How does

Understanding "this" in jQuery and JavaScript

In jQuery, "this" typically refers to the DOM element that is associated with the function being called. For instance, in an event callback, "this" represents the element that triggered the event.

Example:

<code class="javascript">$("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";
});</code>

jQuery also uses "this" in functions like html() and each():

Example (html):

<code class="javascript">$("#foo div").html(function() {
  // Here, `this` will be the DOM element for each div element
  return this.className;
});</code>

Example (each):

<code class="javascript">jQuery.each(["one", "two", "three"], function() {
  // Here, `this` will be the current element in the array
  alert(this);
});</code>

"this" in Generic JavaScript

Outside of jQuery, "this" in JavaScript generally refers to an object. However, this is not strictly true in ES5's strict mode, where "this" can have any value.

The value of "this" in a function call is determined by how the function is invoked. It can be explicitly set by calling the function through an object property, or it can default to the global object (window in browsers).

Example:

<code class="javascript">var obj = {
  firstName: "Fred",
  foo: function() {
    alert(this.firstName);
  }
};

obj.foo(); // alerts "Fred"</code>

In this example, "this" is explicitly set to the obj object, so it can access the firstName property.

However, it's important to note that the function foo is not inherently tied to any specific object. It can be called with a different "this" value using functions like .call and .apply:

<code class="javascript">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"</code>

In this example, foo is called with the obj object as "this," allowing it to access the firstName property.

ES5's strict mode introduces further complexity, allowing "this" to have non-object values like null, undefined, or primitives like strings and numbers:

<code class="javascript">(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);
  }
})();</code>

Output:

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

In strict mode, "this" is determined by the call site rather than the definition of the function, and it can have non-object values.

The above is the detailed content of How does \"this\" behave differently in jQuery and regular JavaScript, and what are the implications for working with DOM elements and function calls?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn