-
交互式爱情
const obj = { name: " jsCoder", skill: ["es6", "react", "angular"], say: function () { this.skill.forEach(function(item,index){ setTimeout(function(){ console.log(index) console.log(item); },0) }) }, say2:function(){ for (var i = 0, len = this.skill.length; i < len; i++) { setTimeout(function(i,item){ console.log(i); console.log(item); },0,i,this.skill[i]); } }, say3:function(){ const arr=this.skill.slice(); let i=0; function pop(){ const item=arr.shift(); if(item){ console.log(i); console.log(item); i++; setTimeout(pop,0); } } setTimeout(pop,0); }};obj.say();setTimeout(function(){ obj.say2();},1000)setTimeout(function(){ obj.say3();},2000)
-
慕妹3242003
say: function () { var that = this; for (var i = 0, len = this.skill.length; i < len; i++) { setTimeout(function() { console.log(i); console.log(that.skill[i]); }(i), 0) console.log(i); } }
-
一只名叫tom的猫
say: function () { for (var i = 0, len = this.skill.length; i < len; i++) { setTimeout(((i) => { return () => { console.log(i); console.log(this.skill[i]); } })(i), 0) console.log(i); }}
-
MM们
function passTheRiver(){const obj = { "count":0, name: " jsCoder", skill: ["es6", "react", "angular"], say: function () { var that = this; for (var i = 0, len = this.skill.length; i < len; i++) { setTimeout(function(){ console.log(that.count); console.log(that.skill[that.count]); that.count++; }, 0) console.log(i); } }};obj.say();