Skip to content
Discussion options

You must be logged in to vote

Array.prototype 메서드

Array.prototype은 JavaScript 배열 객체의 프로토타입 객체를 나타낸다. 배열의 모든 인스턴스는 이 프로토타입을 상속받아서 배열 관련 메서드와 프로퍼티를 사용할 수 있다.[filter, map, reduce, slice, splice...]

const array = Array.prototype.slice.call(arrayLikeObj);
console.log(array); // ['apple', 'banana', 'orange']
  • Array.prototype.slice 메서드
    slice 메서드는 배열의 일부분을 추출하여 새로운 배열을 반환한다. 이 메서드는 두 개의 인자를 받을 수 있는데, 첫 번째는 추출을 시작할 인덱스이고, 두 번째는 추출을 끝낼 인덱스이다.
  • .call(arrayLikeObj)
    JavaScript의 모든 함수는 this 값을 가지며, call 메서드를 사용하면 함수의 this 값을 특정 값으로 설정할 수 있다. 여기서는 slice 메서드를 호출하는데, thisarrayLikeObj로 설정하여 유사 배열 객체를 대상으로 slice 메서드를 실행한다.
  • 결과
    slice 메서드를 호출하면서 thisarrayLikeObj로 설정하면, slice 메서드는 arrayLikeObj를 배열로 간주하여 배열의 메서드처럼 동작하게 된다. 결과적으로 새로운 배열이 반…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Lv1GoM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
More
Labels
1 participant