여기에 다른 활동의 값을 세는 switch 문을 가지고있는 주 등 여러 번 여러 번 일하는 것과 같은 일일 활동을 사람마다 계산하는 프로그램이 있습니다. 나는이 물체가 sort()
함수 안에서 dayEvents object
을 나타낼 것을 강요했다. 그러나 forEach method
안에는 전역 물체를 나타내는 그것도있다. 또한 축구가 정의되지 않았다는 오류가있다. 왜 그런가? 나는 어떻게 강제 할 수 있는가? 동일한 dayEvents
개체를 나타내는?여기에 원하는 개체를 나타 내기 위해 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){
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>
스위치 케이스는 스트링으로되어 있어야합니다. 케이스. "풋볼".. 케이스 "학교"등. – George
@ 조지 : 예, 아마도. '축구', '학교'등의 변수가있는 경우 기술적으로 유효합니다. (자바 스크립트의 '스위치'는 자바 나 C와는 다르다. 케이스의 값은 표현식이 될 수있다.)하지만 아마 맞는 것 같아. :-) –
물론 OP가 어디로 향하고 있는지는 분명하다. 특별한 경우;) – George