Skip to content

Instantly share code, notes, and snippets.

@healim
Last active July 18, 2018 08:32
Show Gist options
  • Save healim/938b74d4be87350f9d9a1989e8c7ca41 to your computer and use it in GitHub Desktop.
Save healim/938b74d4be87350f9d9a1989e8c7ca41 to your computer and use it in GitHub Desktop.
javascript concat 데이터타입에 따라 오브젝트 결정
/**
 * concat은 String, Array 둘다에 있음
 * 값 타입이 배열이면 Array.concat()이 선택되고, 문자열이면 String.concat() 호출됨
 * => 즉 데이터 타입에 따라 오브젝트가 결정된다. (자바스크립트 특징)
 */

let sentenceOne = "hello world".concat(" test")
let sentenceTwo = ["hello", " ", "world"].concat([" test"])
let sentenceThree = "hello world".concat([" test"])

console.log(
  typeof sentenceOne + " : " + sentenceOne,
  typeof "hello world" + " : " + "hello world",
  typeof " test" + " : " + " test")
console.log("--------------------------")

console.log(
  Array.isArray(sentenceTwo) + " : " + sentenceTwo,
  Array.isArray(["hello", " ", "world"]) + " : " + ["hello", " ", "world"],
  Array.isArray([" test"]) + " : " + [" test"])
console.log("--------------------------")

console.log(
  typeof sentenceThree + " : " + sentenceThree,
  typeof "hello world" + " : " + "hello world",
  Array.isArray([" test"]) + " : " + [" test"])
console.log("--------------------------")

QQ. concat에서 형변환 어떻게 하는지

QQ. typeof 연산자 어떻게 동작하지? (primitive만 가능한건가 했는데 그건 또 아닌 것 같고(function은 판별 해준다), Number.isNaN(), Array.isArray().. 얘네는 왜 이걸로 판별해야하는지)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment