반응형
루프 내에서 함수를 만들지 마십시오.
이 질문에 이미 답변이 있습니다.
이 경우 jslint 오류를 해결하는 올바른 방법은 무엇입니까? 이것을 사용하는 객체에 getter 함수를 추가하고 있습니다. 루프 내부에 함수를 생성하지 않고이 작업을 수행하는 방법을 모르겠습니다.
for (var i = 0; i<processorList.length; ++i) {
result[i] = {
processor_: timestampsToDateTime(processorList[i]),
name_: processorList[i].processorName,
getLabel: function() { // TODO solve function in loop.
return this.name_;
}
};
}
함수를 루프 밖으로 이동합니다.
function dummy() {
return this.name_;
}
// Or: var dummy = function() {return this.name;};
for (var i = 0; i<processorList.length; ++i) {
result[i] = {
processor_: timestampsToDateTime(processorList[i]),
name_: processorList[i].processorName,
getLabel: dummy
};
}
... 또는 파일 맨 위에 있는 loopfunc
옵션 을 사용하여 메시지를 무시 하십시오.
/*jshint loopfunc:true */
참고 URL : https://stackoverflow.com/questions/10320343/dont-make-functions-within-a-loop
반응형
'developer tip' 카테고리의 다른 글
boost :: shared_ptr 사용에서 std :: shared_ptr로 전환해야합니까? (0) | 2020.11.15 |
---|---|
동일한 "id"를 가진 여러 요소가있을 때 jQuery는 어떻게 작동합니까? (0) | 2020.11.15 |
리눅스 / proc / loadavg (0) | 2020.11.15 |
첫 번째 명령을 일시 중단하더라도 (Ctrl-z) 명령을 하나씩 실행합니다. (0) | 2020.11.15 |
ggplot에서 패싯 순서 수정 (0) | 2020.11.15 |