728x90

✨ Array.prototype.name() 을 지정하여 내가 원하는 helper method를 만들 수 있다.
(e.g., Array.prototype.name()) 왼쪽처럼 선언 후 arr.name()로 method를 실행시킬 수 있다.

💻Example Code

const arr = [ 1, 2, 3, 4, 5 ];

Array.prototype.sumTest = function(){
return this.reduce((acc, val) => {
return acc + val;
})
};

console.log( arr.sumTest() );

실행 결과(1+2+3+4+5=15)

😋 생활코딩에서 학습했었지만, 백준 4673번 문제를 풀면서 실전에서 사용해본 방법이다.

👉 실전에서 자주 사용할 수 있도록 조금 더 연습하고 이해할 필요가 있다.

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

new String()  (0) 2019.12.29
new Array()  (0) 2019.12.29
Array.prototype.fill()  (0) 2019.12.29
String.prototype.replace()  (0) 2019.12.29
Object.keys() & Object.values()  (0) 2019.12.29

+ Recent posts