In Angular. JS, this points to details, and angular. jsthis points

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.