The This in JavaScript

Source: Internet
Author: User
Tags event listener

The This in the ①javascript function represents the context in which the function is called. The following example:

var mm = (function () {var a = function () {console.log (this);//Arguments.callee ();//equivalent to arguments object call a, point to arguments object}; var B = function () {var res = a ();//equals a.call (window), then Window};return {a:a,b:b}}) () Mm.a (); The//mm object calls a method, and a points to mm object in a. Mm.b ();

The ②JAVASCRIPRT literal declares the object in this direction to the object itself.

var mm = {A:function () {console.log (this);},b:function () {var res = THIS.A ();//this points to mm object itself}}MM.A ();//mm object calls a method, A pointer to the MM object in a. Mm.b ();

The This in the ③javascript constructor points to the instance object created by the constructor. (Note that it does not point to the constructor itself)

function Httpobj () {///Note the This in the constructor points to the generated instance object instead of the constructor itself,//If you use This.prototype = new XMLHttpRequest () in it; You should operate on the constructor Httpobj.prototype = new XMLHttpRequest ();//In fact the this instance object does not exist prototype prototype, its constructor has prototype prototype// The This instance has a private member defined within its constructor, and the member Console.log (This.prototype)//undefined in the prototype that inherits its constructor  does not exist if (window. XMLHttpRequest) {return new XMLHttpRequest ();//If the constructor inside return then the object that the new is constructed is not the instance object to which this is directed//But the value of return} else if (window. ActiveXObject) {var versionss = [' msxml2.xmlhttp.6.0 ', ' msxml2.xmlhttp.3.0 ', ' MSXML2. XMLHttp '];for (var i = 0; i < versionss.length; i++) {try {return new ActiveXObject ()} catch (e) {}}throw new Error ("your The XHR object is not supported by the browser);} else {throw new Error ("Your browser does not support XHR objects");}}

The ④javascript event listener this points to the DOM element that triggered the event.

Document.addeventlistener ("click", Function (e) {console.log (this);//point to the DOM element that triggered the event})

  

This in JavaScript

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.