Home > Web Front-end > JS Tutorial > body text

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
Release: 2024-10-26 08:18:02
Original
837 people have browsed it

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>
Copy after login

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>
Copy after login

Example (each):

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

"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>
Copy after login

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>
Copy after login

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>
Copy after login

Output:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!