You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
然后实验下面每一个尝试去理解 var reciprocal = window.setInterval(reciprocal, 1000);正常 var reciprocal = window.setInterval(this.reciprocal, 1000);this指向window对象 var reciprocal = window.setInterval("reciprocal()", 1000); reciprocal is not a function,其实也就是window.count() var reciprocal = window.setInterval(self.back(), 1000); 当前实例的back
The text was updated successfully, but these errors were encountered:
先简单写一个用
setTimeout
和clearInterval
配合写的例子setInterval
传递了一个匿名的函数,当然这个匿名函数可以把它写出来例如这样:这样同样是可行的,注意是写函数名就可以了
下面我主要讲讲一些细节我们把上面的代码改造成这样
注意下输出的第一个this和第二个this有什么不同,原因在于window.setInterval的对象是window所以在setInterval里面this是指向window的,而back是指向getCodes这个对象的
![image](https://cloud.githubusercontent.com/assets/17243165/14729969/f0e2d664-0874-11e6-9214-beccc9f426f4.png)
为了让window.setInterval里面的this指向getCodes我们可以更改成这样
用self把this带进setInterval,这样里面的self就是指向getCodes了
var self = this;
我们再往下实验,把setInterval函数里面的匿名函数放出来定义
然后实验下面每一个尝试去理解
var reciprocal = window.setInterval(reciprocal, 1000);
正常var reciprocal = window.setInterval(this.reciprocal, 1000);
this指向window对象var reciprocal = window.setInterval("reciprocal()", 1000);
reciprocal is not a function,其实也就是window.count()var reciprocal = window.setInterval(self.back(), 1000);
当前实例的backThe text was updated successfully, but these errors were encountered: