This in JavaScript points to

Source: Internet
Author: User

---restore content starts---

This point

1, this points to the object that is bound when the function is called. When there is no bound object, point to Windows

2, in the browser, the global environment of this point to the Windows object.

3, can change the point of this through call/apply, ES5 can change the point of this by bind

function person () {

this.name = ' person ';

this.msg = function (msg) {

msg = MSG | | THIS.name;

Alert (msg);

}

}

function Cat () {

THIS.name = ' cat ';

}

var p = new person ();

var c = new Cat ();

P.msg.call (c, ' one ', ');//cat

4, the arrow function of this, define where, then point to where

function person () {

this.name = ' person ';

this.msg = function (msg) {

msg = MSG | | THIS.name;

Alert (msg);

}

This.init = function () {

Document.onclick = () =>{

Console.log (this);

Under normal circumstances this should point to the document, but at this point it points to the person object

}

}

}

P.init ();

---restore content ends---

This in JavaScript points to

Related Article

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.