본문 바로가기
JavaScript

JavaScript 기초(25)_객체(5)

by DeBanker.K 2021. 10. 22.

1. 표준 내장 객체 

: 자바스크립트 자체에 내장되어있는 객체이다. MDN 검색하여 참고하면서 사용하시면 된다.

 

아래는 대표적인 몇 가지 표준 내장 객체를 소개하고자 한다.

 

Array 객체

const a = new Array('red', 'blue', 'green');

console.log(a, typeof a);
console.log(a instanceof Array);
console.log(a instanceof Object);
[Running] 
[ 'red', 'blue', 'green' ] object
true
true

 

'instanceof'를 통해 'Array' 객체뿐만이 아닌 'Object' 객체와 체인 관계를 알 수 있다.

또한 아래와 같이 객체 리터럴 형식으로도 똑같이 나타낼 수 있다. 

const b = ['red', 'blue', 'green'];

console.log(b, typeof b);
console.log(b instanceof Array);
console.log(b instanceof Object);
[Running] 
[ 'red', 'blue', 'green' ] object
true
true

 

Array 객체 속에 내장되어 있는 'slice' 함수를 써보자.

console.log(b.slice(0,1));
console.log(Array.prototype.slice, Object.prototype.slice);
[Running]
[ 'red' ]
[Function: slice] undefined

'slice(0,1)'의 의미는 0번째부터 1개 값만 잘라서 가져오겠다는 의미이다. 

그래서 아래에 'red'만 출력되었다. 

또한 두번째 코드를 보면 Array 객체의 프로토타입에는 slice가 내장되어 있지만, Object에는 내장되어 있지 않음을 알 수 있다. 

 

 

댓글