在JavaScript中this是一个永恒的话题,正在可以彻底掌握其实不易,本文主要讲解下一下几种状况下的断定:javascript
一个在模块或者全局的状况下调用一个方法时【不在使用 'use strict'的状况,不然不会默认将this指向全局】php
var x = 9; function test(){ this.x = 6; } test(); alert(x); //输出6
能够知道,在这种状况下的this通常是window或者node中的global;html
这里多是方法做为构造函数来使用,那么这个方法中的this会指向这个构造方法的实例,java
function test(){ this.x = 1; } var o = new test(); alert(o.x); // 1
能够看到,在一些状况下若是出现了相似的调用写法的话,this通常指向的就是调用者自己,形如【object.fn()】,这种状况下的fn里面的this通常指向前面调用fn方法的object对象;node
apply()和call()是函数对象的一个方法,它的做用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象。所以,this指的就是这第一个参数。这里就不讲述二者的不一样了【基本就是两个函数的参数使用不一致】;jquery
var x = 0; function test(){ alert(this.x); } var o={}; o.x = 1; o.m = test; o.m.apply(); //0 o.m.apply(o); //1
ECMAScript 5 引入了 Function.prototype.bind。调用f.bind(someObject)会建立一个与f具备相同函数体和做用域的函数,可是在这个新函数中,this将永久地被绑定到了bind的第一个参数,不管这个函数是如何被调用的。也和call、apply用相同做用;
以上参考了:http://www.ruanyifeng.com/blo...
https://developer.mozilla.org...ajax
在使用js操做dom的时候,很常见的会绑定事件;好比下面的将元素p绑定click事件,而后事件的回调函数中就可使用this;this指向这个时间的目标元素,也就是绑定事件的具体元素;json
function bluify(e){ console.log(this === e.currentTarget); // 老是 true // 当 currentTarget 和 target 是同一个对象是为 true console.log(this === e.target); this.style.backgroundColor = '#A5D9F3'; } // 获取文档中的全部元素的列表 var elements = document.getElementsByTagName('p'); // 将bluify做为元素的点击监听函数,当元素被点击时,就会变成蓝色 for(var i=0 ; i<elements.length ; i++){ elements[i].addEventListener('click', bluify, false); }
而在jQuery的使用中咱们基本就是使用$(this)来表示事件的目标元素;由于this就是e.target;而把一个dom元素变为jQuery元素就是使用$把他抱起来就行了;app
在js中,window实现了WindowOrWorkerGlobalScope这个mixin的全部方法,其中最被常用的就是setTimeout、setInterval,由于这是一个异步的过程:dom
var x = 33; var test = { x: 22, test:function(){ console.log(this.x) }, time:function(){ setTimeout(this.test,1000); } } test.time(); //输出33,不是22
在使用异步的setTimeout、setInterval时候,因为须要等待特定时间以后在进行执行,因此这个就和setTimeout、setInterval的执行有关,在https://zhuanlan.zhihu.com/p/... 和 https://jakearchibald.com/201... 中能够知道,其实就是event loop的概念,因此其实在使用setTimeout、setInterval的时候会出现等待的时长并非精准的参数中写入的时间,缘由以下:
故而解决方法以下:
this.intervalID = setInterval( (function(self) { //Self-executing func which takes 'this' as self return function() { //Return a function in the context of 'self' self.retrieve_rate(); //Thing you wanted to run as non-window 'this' } })(this), this.INTERVAL //normal interval, 'this' scope not impacted here. );
在ajax中咱们都会很是谨慎的使用this;这个异步操做在this上也是和setTimeout差很少,确定是会被覆盖的,可是并非被window覆盖,相反而是被XMLHttpRequest对象覆盖:
{ "url": "demo_test.txt", "type": "GET", "isLocal": false, "global": true, "processData": true, "async": true, "contentType": "application/x-www-form-urlencoded; charset=UTF-8", "accepts": { "*": "*/*", "text": "text/plain", "html": "text/html", "xml": "application/xml, text/xml", "json": "application/json, text/javascript", "script": "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript" }, "contents": { "xml": {}, "html": {}, "json": {}, "script": {} }, "responseFields": { "xml": "responseXML", "text": "responseText", "json": "responseJSON" }, "converters": { "text html": true }, "flatOptions": { "url": true, "context": true }, "jsonp": "callback", "dataTypes": [ "text" ], "crossDomain": false, "hasContent": false }
上面是一个ajax中的this的json格式,测试地址:http://www.runoob.com/try/try... 只须要在ajax的回调中打印this【console.log(JSON.stringify(this))】就能够在开发者工具的console中看到this这面目,也就是上面的格式的数据。