/ FRONTEND

클린코드 자바스크립트 2-1 타입 다루기(타입 검사)

Clean Code JS 강좌는 여러 절로 구성되어 있습니다.

타입 다루기 😎

타입 검사👍

  • PRIMITIVE vs REFERENCE
  • Recference 는 객체 (Array, function, Date …)
  • typeof 로 감별해내기 어렵다.
function myFunction() {}
class MyClass {}
const str = new String("문자열");
typeof "문자열"; // 'string'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof 123; // 'number'
typeof Symbol(); // 'symbol'

typeof myFunction; // 'function'
typeof MyClass; // 'function
typeof str; // 'undefined'

가장 치명적인 문제 😥
자바 스크립트가 인정한 오류

typeof null; // 'object'
  • 자바스크립트는 동적으로 변하는 언어 ▻ 타입 동적
  • 객체의 프로토타입 체인을 확인하는 법 (객체 확인에 용이)
function Person(name, age) {
  this.name = name;
  this.age = age;
}
const p = {
  name: "test",
  age: 85,
};
const poco = new Person("poco", 99);
poco instanceof Person; // true
p instanceof Person; // false

const arr = [];
const func = function () {};
const date = new Date();

arr instanceof Array; // true
func instanceof Function; // true
date instanceof Date; // true

// 최상위에 Object 가 있으므로 true
arr instanceof Object; // true
func instanceof Object; // true
date instanceof Object; // true

Object.prototype.toString.call(arr); // '[object Array]'
Object.prototype.toString.call(func); // '[object Function]'
Object.prototype.toString.call(date); // '[object Date]'
  • 자바스크립트는 동적인 타입을 가진 언어이다.
  • 타입 검사가 어려움 하나하나 잘 찾아가면서 검사 해야한다.
  • 어떠한 부분이 주의해야할지 외우는게 아닌 하나하나 잘 찾아서 검사해야한다!
  • Primitive vs Reference / typeof instanceof 등

Solution 🌼

아래와 같은 내용으로 Stack Overflow , Google 최신 검색해보며 타입 검사

  • javascript is function
  • javascript is Array
  • javascript is String

}