This article mainly introduces the this attribute in JavaScript. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
This always returns an object , that is, returns the property or method where is currently located object.
The properties of an object can be assigned to another object, so the current object where the properties are located is variable, that is, the point of this is variable.
eg:
1 2 3 4 5 6 7 8 9 10 11 |
|
Result: "Name: Li Si"
Look at another example:
1 2 3 4 5 6 7 8 9 |
|
The result is also "Name: Li Si", because at this time this points to the object where f is running - the top-level window
Usage occasions of this
1. Global environment ——No matter whether this is inside the function or not, as long as it is run in the global environment, this refers to the top-level object window
2. Constructor ——Refers to the instance object
eg:
1 2 3 4 5 6 7 8 9 |
|
The result is that "color" is returned
The above code defines a constructor Obj. Since this points to the instance object, defining this.p in Obj is equivalent to defining an instance object. p attribute, and then the m method can return this p attribute.
3. Object methods
1 2 3 4 5 6 |
|
Only when the foo method is directly called on the obj object, this will point to obj. In other uses, this will point to the object where the code block is currently located.
Case 1: (obj.foo = obj.foo)()——window
Case 2: (false || obj.foo)()——window
Case 3: (1, obj.foo)()——window
In the above code, obj.foo is calculated first and then executed. Even if its value does not change, this no longer points to obj
4. Node
In Node, if it is in the global environment, this points to global, and in the module environment, this points to module.exports
Notes on using this
1. Avoid multiple layers of this
1 2 3 4 5 6 7 8 9 |
|
The execution result is:
{f1: ƒ}
Window {decodeURIComponent: ƒ, postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, …}
Why does this in f2 point to the global object? Because the execution process of the above code is actually
1 2 3 4 5 6 7 8 9 10 |
|
Solution 1 - Use a variable pointing to the outer this in the second layer
1 2 3 4 5 6 7 8 9 10 |
|
Use a variable to fix the value of this , and then the inner layer calls this variable, which is a very useful and widely used method.
Solution 2 - Use strict mode. In strict mode, if this inside the function points to the top-level object, an error will be reported.
2. Avoid using this
1 2 3 4 5 6 7 8 9 10 |
|
in the array processing method. Result:
undefined a1
undefined a2
leads to this result. The reason is the same as the multi-layer this in the previous paragraph.
Solution one - use intermediate variables
1 2 3 4 5 6 7 8 9 10 11 |
|
Solution two - treat this as the second parameter of the forEach method, fixed Its running environment
1 2 3 4 5 6 7 8 9 10 |
|
3. Avoid this in the callback function
1 2 3 4 5 6 |
|
How to bind this
JavaScript provides call, apply, bind has three methods to switch/fix the pointer of this
function.prototype.call()
The call method of a function instance can specify the role of this when the function is executed. Domain, the parameter of the call method is an object. If the parameter is empty, null, or undefined, the global object will be passed in by default. If the call parameter is not an object, it will be automatically wrapped into a wrapping object. func.call(thisValue,arg1,arg2,...)
1 2 3 4 5 6 7 8 9 10 11 |
|
An application of the call method is to call the native method of the object
1 2 3 4 5 6 7 8 9 10 |
|
function.prototype.apply()
The only difference between apply and call is that apply accepts an array as a parameter when the function is executed, func.apply(thisValue,[arg1,arg2,...])
The application of apply One - Find the largest element of the array
1 2 |
|
Apply application two - Change the empty elements of the array to undefined (because the forEach method of the array will skip empty elements, but will not skip undefined) ?
1 2 3 4 5 6 |
|
The running results are not the same as above, they are all a b
Apply application 3 - Convert array-like objects
1 |
|
Apply application 1 Four - the object of the bound callback function
1 2 3 4 5 6 7 8 |
|
function.prototype.bind()
bind method is used to bind this in the function body to an object and then return a new function
The following example will cause an error after assigning a value to the method
1 2 3 4 |
|
Solution:
1 |
|
bind is a step further than call and apply. In addition to binding this, it also You can bind the parameters of the original function
1 2 3 4 5 6 7 8 9 |
|
For those old browsers that do not support the bind method, you can define the bind method yourself
1 2 3 4 5 6 7 8 9 10 |
|
The above is the detailed content of Detailed explanation of this attribute in JavaScript. For more information, please follow other related articles on the PHP Chinese website!