길/Javascript 기본

arguments object과 array like object

7he8oy 2021. 1. 28. 11:34
function getAllParamsByRestParameter(...args) {
      return args;
    }

function getAllParamsByArgumentsObj() {
      return arguments;                                                              // 기록해두기
    }

const restParams = getAllParamsByRestParameter('first', 'second', 'third');
const argumentsObj = getAllParamsByArgumentsObj('first', 'second', 'third');

 

 

위의 코드에서 restParams는 array를 리턴하고, argumentsObj는 object를 리턴한다.

 

구글 개발자 모드로 둘을 확인해보았더니 어딘가 다르다는 것을 알 수 있었다.

argumentsObj는 array의 탈을 쓴 object로 보인다. 이를 array like object라고 한다.

 

아직 prototype에 대해서 모르지만,

argumentsObj는 _proto_가 값으로 Object를 갖고 있고

restParams는 _proto_의 값이 Array(0)이다. 

또한 argumentsObj는 겉으로 보이지 않는 이상한 값들을 갖고 있다. (nonenuerable?이 이런 것들을 말하는 것 같다.)

 

 

참고로

var ao1 = {length: 0},                     // like []
    ao2 = {0: 'foo', 5: 'bar', length: 6}; // like ["foo", undefined × 4, "bar"]

이런 식으로 array-like object를 생성할 수 있다.

 

자세한건 여기로~

stackoverflow.com/questions/29707568/javascript-difference-between-array-and-array-like-object

 

JavaScript - Difference between Array and Array-like object

I have been coming across the term "Array-Like Object" a lot in JavaScript. What is it? What's the difference between it and a normal array? What's the difference between an array-like object and a

stackoverflow.com

 

' > Javascript 기본' 카테고리의 다른 글

고차 함수(Higher Order Function) / 커링과 클로져 차이  (0) 2021.02.01
일급 객체(first-class)  (0) 2021.02.01
Object.assign과 Shallow/Deep copy  (0) 2021.01.28
in 연산자  (0) 2021.01.27
Hoisting  (0) 2021.01.27