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