부울 변수에 따라 배열에서 첫 번째 (또는 마지막) 요소를 삭제하는 코드 조각이 있습니다. 나는 다음과 같이 수행javascript 메서드 array.shift() undefined를 반환합니다.
{...
console.log("length before " + waypoints.length)
waypoints = this.deleteWaypoint(waypoints)
console.log("length after " + waypoints.length)
...}
deleteWaypoint(waypoints){
if (this.first){
this.first = false;
return waypoints.shift()
} else {
this.first = true;
return waypoints.pop()
}
}
waypoints
특정 길이가 첫 번째 로그 인쇄, 그럼 내가 요소를 삭제하는 방법을 호출하고 두 번째 로그 인쇄는 length after undefined
때. "First"는 참으로 초기화 된 전역 변수입니다. 왜 그럴까요?
즉시 배열이 비어로, 당신이 얻을 정의되지 않은, ([여기]를 설명 https://developer.mozilla.org/en-US/ docs/Web/JavaScript/Reference/Global_Objects/Array/shift) – lumio
['Array.prototype.shift'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)/shift) : _ "shift() 메서드는 배열 **에서 첫 번째 요소를 제거하고 제거 된 요소 **를 반환합니다."_ – Andreas
return 문을 'deleteWaypoint'의'return waypoints '로 업데이트합니다. –