4 回答
TA贡献1712条经验 获得超3个赞
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)
TA贡献1824条经验 获得超6个赞
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);
}
}
TA贡献1906条经验 获得超3个赞
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);
}
}
TA贡献1886条经验 获得超2个赞
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();
添加回答
举报