typeof - 피연산자의 데이터 타입을 문자열로 반환하는 타입 연산자

typeof NaN;             // number
typeof [];              // object
typeof new String();    // object
typeof function () {};  // function
typeof undefined;       // undefined
typeof null;            // object (설계적 결함)
typeof undeclared;      // undefined (설계적 결함)

Object.prototype.toString - 모든 타입의 값의 타입을 알아낼 수 있음

Object.prototype.toString.call(null);           // [object Null]

//타입을 반환하는 함수
function getType(target) {
  return Object.prototype.toString.call(target).slice(8, -1);
}
getType(null);       // Null

instanceof - 피연산자인 객체가 우항에 명시한 타입의 인스턴스인지 여부를 알려줌
이때 타입이란 constructor를 말하며 프로토타입 체인에 존재하는 모든 constructor를 검색하여 일치하는 constructor가 있다면 true를 반환

function Person() {}
const person = new Person();

console.log(person instanceof Person); // true
console.log(person instanceof Object); // true

배열인지 체크하기 위해서는 Array.isArray 메소드를 사용

console.log(Array.isArray([]));    // true
console.log(Array.isArray({}));    // false

유사 배열 객체 - length 프로퍼티를 갖는 객체
ex) 문자열, arguments, HTMLCollection, NodeList 등
유사 배열 객체는 length 프로퍼티가 있으므로 순회할 수 있으며 call, apply 함수를 사용하여 배열의 메소드를 사용할 수도 있음

클래스 기반 객체지향 프로그래밍 언어 - 객체 생성 이전에 클래스를 정의하고 이를 통해 객체(인스턴스)를 생성
프로토타입 기반 객체지향 프로그래밍 언어 - 클래스 없이도 객체 생성 가능

Prototype
자바스크립트의 모든 객체는 자신의 부모 역할을 담당하는 객체와 연결
이것은 마치 객체 지향의 상속 개념과 같이 부모 객체의 프로퍼티 또는 메소드를 상속받아 사용할 수 있게 함
이러한 부모 객체를 Prototype(프로토타입)이라고 함

var student = {
  name: 'Lee',
};

// student에는 hasOwnProperty 메소드가 없지만 아래 구문은 동작한다.
console.log(student.hasOwnProperty('name')); // true

[[Prototype]]
함수를 포함한 모든 객체가 가지고 있는 인터널 슬롯
객체의 입장에서 자신의 부모 역할을 하는 프로토타입 객체를 가리키며 함수 객체의 경우 Function.prototype를 가리킴

console.log(Person.__proto__ === Function.prototype);

prototype 프로퍼티
함수 객체만 가지고 있는 프로퍼티
함수 객체가 생성자로 사용될 때 이 함수를 통해 생성될 객체의 부모 역할을 하는 객체(프로토타입 객체)를 가리킴

console.log(Person.prototype === foo.__proto__);

constructor 프로퍼티

function Person(name) {
  this.name = name;
}

var foo = new Person('Lee');

// Person() 생성자 함수에 의해 생성된 객체를 생성한 객체는 Person() 생성자 함수이다.
console.log(Person.prototype.constructor === Person);

// foo 객체를 생성한 객체는 Person() 생성자 함수이다.
console.log(foo.constructor === Person);

// Person() 생성자 함수를 생성한 객체는 Function() 생성자 함수이다.
console.log(Person.constructor === Function);
profile
Front-end Developer 💻🔜

0개의 댓글