Core points
this
in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. this
refers to the global object. In a web browser, it is represented by window
. this
keeps the global object; but when calling the object constructor or any of its methods, this
refers to an instance of the object. call()
using methods such as apply()
, bind()
and this
. These methods call the function using the given this
value and parameters. JavaScript is an excellent programming language. This sentence may have been controversial a few years ago, but developers have rediscovered its beauty and elegance. If you don't like JavaScript, it may be because:
this
Keywords are one of the most confusing concepts. In most languages, this
is a reference to the current object that instantiates. In JavaScript, this
usually refers to an object that "owns" the method, but it depends on how the function is called.
Global scope
If there is no current object, this
refers to the global object. In a web browser, it is a top-level object that represents documents, locations, history, and some other useful properties and methods. window
window.WhoAmI = "我是 window 对象"; alert(window.WhoAmI); alert(this.WhoAmI); // 我是 window 对象 alert(window === this); // true
Calling the function
If you are calling a function, Keep the global object: this
window.WhoAmI = "我是 window 对象"; function TestThis() { alert(this.WhoAmI); // 我是 window 对象 alert(window === this); // true } TestThis();
Calling the object method
When calling an object constructor or any of its methods, refers to an instance of an object—just like any class-based language: this
window.WhoAmI = "我是 window 对象"; function Test() { this.WhoAmI = "我是 Test 对象"; this.Check1 = function() { alert(this.WhoAmI); // 我是 Test 对象 }; } Test.prototype.Check2 = function() { alert(this.WhoAmI); // 我是 Test 对象 }; var t = new Test(); t.Check1(); t.Check2();
Use or call
apply
Essentially,
run JavaScript functions as if they were another object's method. A simple example can be further explained: call
apply
function SetType(type) { this.WhoAmI = "我是 " + type + " 对象"; } var newObject = {}; SetType.call(newObject, "newObject"); alert(newObject.WhoAmI); // 我是 newObject 对象 var new2 = {}; SetType.apply(new2, ["new2"]); alert(new2.WhoAmI); // 我是 new2 对象
can pass an array of parameters. This is the brief description of call
! However, there are some things to be aware of that can get you in trouble. We will discuss these in my next post…apply
this
in JavaScript keyword in JavaScript is a special keyword that refers to the context of the calling function. It is a reference to the object to which the function belongs. The value of will change according to its usage context. In a method, refers to the HTML element that receives the event. This allows you to access and manipulate the element directly in the event handler function. For example, if you have a button with the differently than the normal function. In the arrow function, always refers to an instance of the current class. However, in JavaScript, the value of using methods such as keyword, In strict mode, the value of Yes, Global objects in JavaScript vary by environment. In a web browser, the global object is a this
What are the keywords in JavaScript? Why is it important?
The this
this
depends on how the function is called. It is important because it allows you to access the properties and methods of objects in functions, making your code more flexible and reusable. this
How do keywords work in different contexts?
this
The value of this
refers to the owner object. When used alone, this
refers to a global object. In a function, this
refers to a global object. In an event, this
refers to the element that receives the event. this
works in event handlers?
In an event handler, this
this
event, onclick
in the onclick
function will refer to the button element. this
How does it behave in arrow functions?
The arrow function handles this
this
is lexical bound. This means it uses this
in the code containing the arrow function. Therefore, for arrow functions, this
maintains the value of this
that encloses the lexical context. this
in JavaScript and
In many object-oriented programming languages, this
in other programming languages? this
this
is determined by the execution context and the way the function is called, rather than by the definition location of the function. this
in a function?
You can change the context of this
call()
, apply()
and bind()
. The this
and call()
methods call the function using the given apply()
value and parameters. The difference between the two is that this
accepts a parameter list, while call()
accepts a single parameter array. The apply()
method returns a new function that allows you to bind the function to a specific object. bind()
What is the value of
refers to the newly created object. When you create an instance of an object using the this
In the constructor, this
is automatically set to the new object. new
this
How do you behave in strict mode? this
remains at the value set when entering the execution context. If undefined, it remains undefined. This is different from non-strict mode, where this
defaults to global objects if undefined.
this
Can it be null or undefined? this
can be null or undefined, especially when calling a function in strict mode or using call()
or apply()
with undefined or null as the first argument. What are the global objects in JavaScript?
window
object. In Node.js, a global object is a global object literal. When this
is used outside of any function or method, it refers to a global object.
The above is the detailed content of What is 'this' in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!