这里有一个计算一个人不同的日常活动的程序,比如他在一周内踢了多少次足球,etc.Here,我有一个交换语句,它计算不同活动的值。我强迫这个对象指示dayEvents object in sort() function.But insort()function.But in forEach method,它的一个错误是说足球不是defined.why,是这样吗?我怎么能强迫它来指示同一个dayEvents对象呢?
<html>
<body>
<script>
(function(){
var dayEvents={
football:0,
school:0,
video_games:0,
watch_tv:0,
coding:0,
container:[],
insert:function(evt){
this.container.push(evt);
},
get:function(){
sort.call(this);
}
}
function sort(){
for(i=0;i<this.container.length;i++){
this.container[i].forEach(function(elements,index,array){
switch(elements){
case football:
(this.football)++;
break;
case school:
(this.school)++;
break;
case video_games:
(this.video_games)++;
break;
case watch_tv:
(this.watch_tv)++;
break;
case coding:
(this.coding)++;
break;
}
});
}
}
dayEvents.insert(['football','school','video_games','watch_tv','coding']);
dayEvents.insert(['football','school','coding']);
dayEvents.insert(['school','video_games','watch_tv','coding']);
dayEvents.insert(['football','video_games','coding']);
dayEvents.insert(['school','watch_tv','coding']);
dayEvents.insert(['football','school','video_games','watch_tv','coding']);
dayEvents.insert(['football','school','video_games','coding']);
dayEvents.get();
console.log(dayEvents.football);
})();
</script>
</body>
</html>发布于 2014-09-08 11:05:58
向forEach提供第二个参数,称为thisArg;它将在您的迭代器回调期间将其用作this。
我认为您正在寻找具有与外部相同的迭代器内部调用的this,因此:
this.container[i].forEach(function(elements,index,array){
/* ...contents omitted for brevity... */
}, this);
// ^^^^^^或者,您可以通过Function#bind使用绑定函数,但是由于forEach专门为您提供了thisArg,这可能是可行的方法。(所有其他的ES5数组添加都有它,FYI- some,every,reduce,.)
另外,正如George在关于这个问题的评论中指出的,您的case标签的值有点可疑:
case football:这在JavaScript中是有效的,如果您有一个名为football的变量(这可能会让一些人感到惊讶!),但是看看您的代码,我怀疑您可能想要:
case "football":对于其他人,...and也是类似的。
发布于 2014-09-08 11:29:22
要在forEach循环中获得所需的对象,需要将该对象作为forEach函数中的第二个参数传递。在“案例足球:”中,你会得到这个错误。因为足球不是一个变量,所以你需要用它作为一个字符串。
请使用下面的代码。
<html>
<body>
<script>
(function(){
var dayEvents={
football:0,
school:0,
video_games:0,
watch_tv:0,
coding:0,
container:[],
insert:function(evt){
this.container.push(evt);
},
get:function(){
sort.call(this);
}
}
function sort(){
for(i=0;i<this.container.length;i++){
this.container[i].forEach(function(elements,index,array){
console.log(this);
switch(elements){
case 'football':
(this.football)++;
break;
case 'school':
(this.school)++;
break;
case 'video_games':
(this.video_games)++;
break;
case 'watch_tv':
(this.watch_tv)++;
break;
case 'coding':
(this.coding)++;
break;
}
}, this); //instead of 'this', you can also pass 'dayEvents'
}
}
dayEvents.insert(['football','school','video_games','watch_tv','coding']);
dayEvents.insert(['football','school','coding']);
dayEvents.insert(['school','video_games','watch_tv','coding']);
dayEvents.insert(['football','video_games','coding']);
dayEvents.insert(['school','watch_tv','coding']);
dayEvents.insert(['football','school','video_games','watch_tv','coding']);
dayEvents.insert(['football','school','video_games','coding']);
dayEvents.get();
console.log(dayEvents.football);
})();
</script>
</body>
</html>希望这能有所帮助。如果能用,请告诉我。
https://stackoverflow.com/questions/25722832
复制相似问题