In Angular. JS, this points to details, and angular. jsthis points
[This details]
1. Who calls the function and who points to this.
① This points to, and can always be an object !!!!!!
② Who this points to will never depend on where this is written !! It depends on where the function is called.
③ This indicates the object, which is called the context of the function, or the caller of the function.
2. ※※※※※this refers to the rule (which is closely related to the function call method ):
This points to the situation, depending on the function call method:
① Direct call by function name (): this points to window
② Called through the object. Function Name (): this points to this object
③ As an element of the array, the function is called through the array Subscript: this points to this array
④ The function is called as the callback function of the window built-in function: this points to window setInterval setTimeout and so on...
⑤ Function as the constructor. When calling with the new Keyword: this points to the new object.
function func(){ console.log(this); }
① Direct call by function name (): this points to window
func(); this--->window
② Called through the object. Function Name (): this points to this object
Narrow object
var obj = { name:"obj", func1 :func }; obj.func1(); this--->obj
Generalized object
document.getElementById("div").onclick = function(){ this.style.backgroundColor = "red"; }; this--->div
③ As an element of the array, the function is called through the array Subscript: this points to this array
var arr = [func,1,2,3];arr[0](); this--->arr
④ The function is called as the callback function of the window built-in function: this points to the window
setTimeout(func,1000);// this--->windowsetInterval(func,1000);
⑤ Function as the constructor. When calling with the new Keyword: this points to the new object.
Var obj = new func (); // this ---> new obj
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.