πŸ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • μ½œλ°±ν•¨μˆ˜ (Callback function)
    • ν•¨μˆ˜μ˜ 호좜
    • 자료ꡬ쑰 new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • 객체
    • Arguments
    • higher order function
    • operators-function-control-flow
    • 객체 생성 νŒ¨ν„΄ 3κ°€μ§€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • 데이터 νƒ€μž…
    • Object 객체
    • ν‘œμ€€ λ‚΄μž₯ 객체의 ν™•μž₯
    • μ°Έμ‘°
    • ν•¨μˆ˜
    • 상속(Inheritance)
    • this - 'this'λ₯Ό μ‚¬μš©ν•˜λŠ” ν•΄λ‹Ή ν•¨μˆ˜λ₯Ό 'μ–΄λ–»κ²Œ' μ‹€ν–‰ν•˜λŠλƒμ— λ”°λ₯Έ 4κ°€μ§€ this μ •μ˜
    • 전역객체(Global object)
    • 객체 μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ°
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - μžμ—°μˆ˜, μ •μˆ˜, 10μ§„μˆ˜, 2μ§„μˆ˜, λΆ€λ™μ†Œμˆ˜μ , μ‹€μˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ™€ 클래슀, μ–΄λ–€ 차이가 μ‘΄μž¬ν• κΉŒ?
    • Hooksκ³Ό useEffect μ‚¬μš©ν•΄ 보기
    • Route
    • async wait μ‚¬μš©ν•˜κΈ°
    • Hooks API Reference
    • context
    • npm uninstall ν•˜λŠ”λ²•
    • test λ§Œλ“€κΈ°
  • tip
    • ν΄λ¦°μ½”λ“œ
    • BxSlider둜 ν…μŠ€νŠΈ 흐λ₯΄λŠ” 효과 λ§Œλ“€κΈ°
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • μš°μ•„ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ 2λΆ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • μš°μ•„ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • λ¨Έμ§€ ν›„ branch μ‚­μ œν•˜κΈ°
    • μ§€μ˜₯μ—μ„œ 온 Git
    • reset-revert
    • develop branch λ₯Ό pull ν•˜κ³  μ‹Άμ„λ•Œ
  • conference-and-seminar
    • λͺ¨λ˜ ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œν™˜κ²½μ˜ 이해
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • μ•Œκ³ λ¦¬μ¦˜ μ½”λ“œλ¦¬λ·°
    • 슀슀둜 κ³΅λΆ€ν•˜λŠ” 법
    • 바닐라코딩 μˆ˜κ°• ν›„κΈ°
    • async 과제 ν›„κΈ° - 비동기, 동기, ν΄λ‘œμ €, λ°°μ—΄κ³Ό 객체
    • μœ μš©ν•œ μ‚¬μ΄νŠΈ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars 이용자 μ†”λ£¨μ…˜ λͺ¨μŒ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • νƒ€μž…κ³Ό 좔상화
      • 객체 지도
      • μ΄μƒν•œ λ‚˜λΌμ˜ 객체
      • 좔상화 기법
      • 05. μ±…μž„κ³Ό λ©”μ‹œμ§€
      • 07.ν•¨κ»˜ λͺ¨μœΌκΈ°
      • 04. μ—­ν• , μ±…μž„, ν˜‘λ ₯
      • ν˜‘λ ₯ν•˜λŠ” κ°μ²΄λ“€μ˜ 곡동체
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • μ›Ή λΈŒλΌμš°μ €μ— URL을 μž…λ ₯ν–ˆμ„ λ•Œ μ–΄λ–»κ²Œ μ›Ή νŽ˜μ΄μ§€κ°€ λ³΄μ—¬μ§ˆκΉŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex 해버렸지 뭐야
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd λͺ…λ Ήμ–΄ λͺ¨μŒ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN μ„€μ •ν•˜κΈ°
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans μ˜€λ‹΅λ…ΈνŠΈ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • μ½”λ“œ μŠ€ν”ΌμΈ  - CSS Rendering 1회차 2/2
    • μ½”λ“œ μŠ€ν”ΌμΈ  - CSS Rendering 1회차 1/2
  • you-dont-know-js
    • νƒ€μž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Syntax
  • Parameters
  • Template literals
  • Escape notation
  • Long literal strings
  • Description
  • Character access(λ¬Έμžμ ‘κ·Ό)
  • λ°°μ—΄κ³Ό λ‹€λ₯΄λ‹€
  • Comparing strings(λ¬Έμžμ—΄ 비ꡐ)
  • native
  • Distinction between string primitives and String objects

Was this helpful?

  1. javascript-api

String

Previousstring.toLocaleLowerCaseNextlearn-node

Last updated 5 years ago

Was this helpful?

String μ „μ—­κ°μ²΄λŠ” λ¬Έμžμ—΄μ˜ μƒμ„±μžμ΄λ‹€.

Syntax

λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄μ€ πŸ‘‡

'sting text'
"string text"
"μ˜μ–΄ ν•œκ΅­μ–΄ 기타 λ“±λ“± λ‹€μ–‘ν•œ μ–Έμ–΄λ₯Ό 수용"

'', "" λ₯Ό μ‚¬μš©

global objectλ₯Ό λ°”λ‘œ λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.

String(thing)

Parameters

thing string μ•„λ¬΄κ°’μ΄λ‚˜.

Template literals

ECMAScript 2015λΆ€ν„°λŠ” string literal이 Template Iiteralsκ°€ string literal이 될 수 μžˆλ‹€.

`hello ${world}`

Escape notation

특수문자 ν‘œν˜„ 이런걸 Escape notation 이라고 ν•˜λŠ”κ΅¬λ‚˜ πŸ€”

javascriptλŠ” μž‘μ€ λ”°μ˜΄ν‘œμ™€ 큰 λ”°μ˜΄ν‘œλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 상관없이 λ™μž‘ν•œλ‹€.

μ½”λ“œ

좜λ ₯

\XXX

8μ§„μˆ˜ Latin-1 문자 (where XXX is 1–3 octal digits; range of 0–377)

\'

"\'" => "''" μž‘μ€ λ”°μ˜΄ν‘œ

\"

"\"" => """ 큰 λ”°μ˜΄ν‘œ

\\

"\\" => "\" μ—­μŠ¬λž˜μ‹œ

\n

"\n abc" => " abc" κ°œν–‰

\r

\v

vertical tab πŸ€”

\b

backspace

\f

\uXXXX

unicode codepoint "\u0056" => "V"

\xXX

Latin-1 문자 (where XX is 2 hex digits; range of 0x00–0xFF)

Long literal strings

ν•œμ€„μ„ μ—¬λŸ¬μ€„λ‚΄μ— μž…λ ₯ν•˜κ³  싢을 λ•Œμ—”

+μ—°μ‚°μž ν˜Ήμ€ "\" μ—­μŠ¬λž˜μ‹œλ₯Ό μ΄μš©ν•˜μ—¬ κΈ΄ λ¬Έμžμ—΄μ„ κ°œν–‰ν•˜μ—¬λ„ ν•œμ€„λ‘œ 보여쀄 수 μžˆλ‹€.

let longString1 = "μ—¬λŸ¬ 쀄 μž‘μ„± ν•œλ‹€.. " + 
                                    "μ—¬λŸ¬ 쀄.."
let longString2 = "μ—¬λŸ¬μ€„ μž‘μ„± ν•œλ‹€.. \
                                    μ—­μŠ¬λž˜μ‹œλŠ” μ—­μŠ¬λž˜μ‹œ λ‹€μŒμ— 곡백을 ν¬ν•¨ν•œ \
                  μ–΄λ–€ λ¬Έμžκ°€ μ™€μ„œλŠ” μ•ˆλœλ‹€."

μ—­μŠ¬λž˜μ‹œλ₯Ό μ‚¬μš©ν•  경우 μ—­μŠ¬λž˜μ‹œ λ‹€μŒμ— μ–΄λ–€ λ¬Έμžλ„ μ™€μ„œλŠ” μ•ˆλœλ‹€.

Description

λ¬Έμžμ—΄μ€ ν…μŠ€νŠΈ ν˜•νƒœλ‘œ ν‘œν˜„λ  수 μžˆλŠ” 데이터λ₯Ό λ³΄κ΄€ν•˜λŠ” 데 μœ μš©ν•˜λ‹€. κ°€μž₯ 많이 μ‚¬μš©λ˜λŠ” 것은 length μ—°κ²°ν•˜λŠ” +와 += μ—°μ‚°μž μ„œλΈŒ 문자λ₯Ό ν™•μΈν•˜λŠ” substring, 있으면 μœ„μΉ˜λ₯Ό ν™•μΈν•˜λŠ” indexOf() , μ„œλΈŒ 문자λ₯Ό μΆ”μΆœν•˜λŠ” substring()

Character access(λ¬Έμžμ ‘κ·Ό)

  1. charAt() λ©”μ„œλ“œ μ΄μš©ν•˜κΈ°

    return 'cat'.charAt(1); // returns "a"
  2. ECMAScript 5μ—μ„œ μ†Œκ°œν•˜κ³  μžˆλŠ” λ¬Έμžμ—΄μ„ λ°°μ—΄κ³Ό 같은 object둜 μ·¨κΈ‰ν•˜μ—¬ λ°°μ—΄μ²˜λŸΌ 인덱슀둜 μ ‘κ·Όν•˜κΈ°

    return 'cat'[1]; // returns "a"

    ν•˜μ§€λ§Œ λ°°μ—΄μ²˜λŸΌ μƒˆλ‘œμš΄ 값을 ν• λ‹Ήν•˜κ±°λ‚˜ μ‚­μ œν•  수 μ—†λ‹€.

λ°°μ—΄κ³Ό λ‹€λ₯΄λ‹€

const str = "foo";
const arr = ["f","o","o"];

str.length; // 3
arr.length; // 3

str.indexOf("o"); // 1
arr.indexOf("o"); // 1

const strConcat = str.concat("bar"); // "foobar"
const arrConcat = arr.concat(["b", "a", "r"]); // ["f","o","o,"b","a","r"]

str === strConcat // false
arr === arrConcat // false

str // "foo"
arr // ["f","o","o"]

μ΄λŸ°μ μ„ 보고 λ‘˜ λ‹€ "문자의 λ°°μ—΄"이라고 생각할 수 μžˆμ§€λ§Œ μ•„λ‹ˆλ‹€.

str[1] = "0";
arr[1] = "0";

str; // "foo"
arr; // ["f", "0", "o"]
  1. λ¬Έμžμ—΄μ€ "λΆˆλ³€ κ°’ Immutable"μ΄μ§€λ§Œ 배열은 "κ°€λ³€κ°’ Mutable"이닀.

    str[1]번처럼 νŠΉμ •λ¬Έμžλ₯Ό μ ‘κ·Όν•˜λŠ” ν˜•νƒœκ°€ λͺ¨λ“  μžλ°”μŠ€ν¬λ¦½νŠΈ μ—”μ§„μ—μ„œ μœ νš¨ν•œ 것은 μ•„λ‹ˆλ‹€.

    μ‹€μ œ 인터넷 μ΅μŠ€ν”Œλ‘œλŸ¬ 7은 이λ₯Ό 문법 μ—λŸ¬λ‘œ μΈμ‹ν•œλ‹€.

    str.charAt(1)둜 μ ‘κ·Όν•˜λŠ”κ²Œ λ§žλ‹€.

  2. λ¬Έμžμ—΄μ€ λΆˆλ³€ κ°’μœΌλ‘œ λ¬Έμžμ—΄ λ©”μ„œλ“œλŠ” κ·Έ λ‚΄μš©μ„ λ°”λ‘œ λ³€κ²½ν•˜μ§€ μ•Šκ³  항상 μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ€ μƒμ„±ν•œ ν›„ λ°˜ν™˜ν•œλ‹€. ν•˜μ§€λ§Œ λ°°μ—΄λ©”μ„œλ“œλŠ” κ·Έ μžλ¦¬μ—μ„œ μˆ˜μ •ν•œλ‹€.

λ¬Έμžμ—΄μ„ λ‹€λ₯Ό λ•Œ μœ μš©ν•œ λ°°μ—΄ λ©”μ„œλ“œλŠ” λ¬Έμžμ—΄μ— μ“Έ 수 μ—†μ§€λ§Œ, λ¬Έμžμ—΄μ— λŒ€ν•΄ λΆˆλ³€ λ°°μ—΄ λ©”μ„œλ“œλ₯Ό 빌렀 μ“Έ 수 μžˆλ‹€.

str.join; // undefined
str.map; // undefined

// The join() method creates and returns a new string by concatenating all of the elements in an array
const strJoin = Array.prototype.join.call(str, "-");

// map은 μƒˆλ‘œμš΄ 배열을 returnν•˜κΈ° λ•Œλ¬Έμ— join()ν•˜μ§€ μ•ŠμœΌλ©΄ λ°°μ—΄λ‘œ return
const strMap = Array.prototype.map.call(str, function(v){
  return v.toUpperCase() + ".";
}).join("");

strJoin // "f-o-o"
strMap // "F.O.O."

λ°°μ—΄μ˜ κ°€λ³€ λ©”μ„œλ“œλŠ” μ‚¬μš©ν•  수 μ—†λ‹€.

// The reverse() method reverses an array in place.
Array.prototype.reverse.call(str);
// String 객체 래퍼λ₯Ό λ°˜ν™˜ν•œλ‹€.
str // "foo" :(

λ¬Έμžμ—΄μ€ λΆˆλ³€ 값이라 λ°”λ‘œλ°”λ‘œ λ³€κ²½λ˜μ§€ μ•ŠμœΌλ―€λ‘œ λ°°μ—΄μ˜ κ°€λ³€ λ©”μ„œλ“œλŠ” λ©”μ„œλ“œλŠ” ν†΅ν•˜μ§€ μ•Šκ³ , 빌렀 μ“°λŠ” 것 λ˜ν•œ μ•ˆλœλ‹€.

λ¬Έμžμ—΄μ„ λ°°μ—΄λ‘œ λ°”κΎΈκ³  μ›ν•˜λŠ” μž‘μ—…μ„ μˆ˜ν–‰ν•œ ν›„ λ‹€μ‹œ λ¬Έμžμ—΄λ‘œ λ°”κΎΈλŠ” Hack을 μ‚¬μš©ν•˜κΈ°λ„ ν•œλ‹€. 이 μ‚¬μš©μ€ Codewarsμ—μ„œ 맀우 흔흔.. ν•˜μ§€λ§Œ μ΄λŸ¬ν•œ 방법은 μœ λ‹ˆμ½”λ“œκ°€ μ„žμ—¬ μžˆλŠ” 경우(특수문자, Multibyte λ“±) 이 방법이 ν†΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

생각을 λ‹¬λ¦¬ν•˜μ—¬ μž‘μ—…μ΄ 빈번히 ν•„μš”ν•œ λ¬Έμžμ—΄μ΄λΌλ©΄ 문자 λ‹¨μœ„λ‘œ μ €μž₯ν•˜λŠ” λ°°μ—΄λ‘œ μ·¨κΈ‰ν•˜λŠ”κ²Œ 더 λ‚˜μ„ 수 μžˆλ‹€.

Comparing strings(λ¬Έμžμ—΄ 비ꡐ)

less-thanκ³Ό greater-than μ—°μ‚°μžλ§Œμ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ 비ꡐ할 수 μžˆλ‹€.

const a = "a";
const b = "b";

if(a === b) console.log(`${a} and ${b} are equal.`);
if(a > b) console.log(`${a} is gerater than ${b}`);
if(a < b) console.log(`${a} is less than ${b}`);

String μΈμŠ€ν„΄νŠΈμ— μƒμ†λœ localeCompare() λ©”μ„œλ“œλ₯Ό μ‚¬μš© ν•  수 μžˆλ‹€.

native

νŠΉμ • λΈŒλΌμš°μ €μ— μ’…μ†λ˜μ§€ μ•Šμ€ ECMAScript λͺ…μ„Έμ˜ λ‚΄μž₯객체λ₯Ό λ§ν•œλ‹€. (Window, Button 등은 μ œμ™Έ)

Stirng()을 ν¬ν•¨ν•˜μ—¬ Number(), Boolean(), Array(), Object(), Function(), RegExp(), Dat(), Error(), Symbol() κ°€μž₯ 많이 μ“°λŠ” λ„€μ΄ν‹°λΈŒ λ„€μ΄ν‹°λΈŒλŠ” λ‚΄μž₯ ν•¨μˆ˜μ΄λ‹€.

native prototype

λ‚΄μž₯ λ„€μ΄ν‹°λΈŒ μƒμ„±μžλŠ” 각자의 .prototype 객체λ₯Ό κ°€μ§„λ‹€.(Array.prototype, String.prototype)

prototype κ°μ²΄μ—λŠ” ν•΄λ‹Ή 객체의 ν•˜μœ„ νƒ€μž…λ³„λ‘œ κ³ μœ ν•œ 둜직이 담겨 μžˆλ‹€.

λ¬Έμžμ—΄ μ›μ‹œ 값을 λ°•μ‹±μœΌλ‘œ ν™•μž₯ν•œ κ²ƒκΉŒμ§€ ν¬ν•¨ν•˜μ—¬ λͺ¨λ“  String κ°μ²΄λŠ” 기본적으둜 Stirng.prototype 객체에 μ •μ˜λœ λ©”μ„œλ“œμ— μ ‘κ·Ό ν•  수 μžˆλ‹€. ν”„λ‘œν† νƒ€μž… μœ„μž„(prototype Delegation) 덕뢄에 λͺ¨λ“  λ¬Έμžμ—΄μ΄ λ©”μ„œλ“œλ“€μ„ 같이 μ“Έ 수 μžˆλ‹€.

String은 "λΆˆλ³€ κ°’ Immutable"이기 λ•Œλ¬Έμ— λ¬Έμžμ—΄ 값을 λ³€κ²½ν•˜λŠ” λ©”μ„œλ“œλŠ” μ—†λ‹€. μˆ˜μ •μ΄ μΌμ–΄λ‚˜λ©΄ μƒˆλ‘œμš΄ 값을 μƒμ„±ν•œλ‹€.

ν”„λ‘œν† νƒ€μž…μ„ λ³€κ²½ν•  μˆ˜λ„ μžˆμ§€λ§Œ, λ³€κ²½ν•˜μ§€ μ•ŠλŠ”κ²Œ μ’‹λ‹€.

Distinction between string primitives and String objects

string primitives와 string objectλŠ” μ„œλ‘œ λ‹€λ₯΄κ²Œ μ·¨κΈ‰ν•œλ‹€.

primitive strings

λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄(μž‘μ€ λ”°μ˜΄ν‘œλ‚˜ 큰 λ”°μ˜΄ν‘œ)κ³Ό μƒμ„±μž 없이(new ν‚€μ›Œλ“œ 없이) String 을 ν˜ΈμΆœν•˜μ—¬ λ°˜ν™˜ν•œ λ¬Έμžμ—΄

const stringPrimitive = "string primitve";
const stringObject = new String('string object');

typeof stringPrimitive // "string"
typeof stringObject // "object"

stringPrimitive instanceof String // false
stringObject instanceof String // true

// The toString() 은 λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•˜λŠ” object의 λŒ€ν‘œμ μΈ 방법
// 이 λ©”μ„œλ“œκ°€ μ‚¬μš©μž μ§€μ • κ°œμ²΄μ—μ„œ μž¬μ •μ˜λ˜μ§€ μ•ŠμœΌλ©΄ toString()은 "[object type]"을 λ°˜ν™˜ν•©λ‹ˆλ‹€. μ—¬κΈ°μ„œ type은 object typeμž…λ‹ˆλ‹€. 
Object.prototype.toString.call(stringPrimitive) // "[object String]"
Object.prototype.toString.call(stringObject) // "[object String]"

new String('string object') μƒμ„±μžμ˜ κ²°κ³ΌλŠ” μ›μ‹œκ°’ 'string object'λ₯Ό 감싼 object wrapper 이닀.

typeof stringObject // "object" 이 κ²°κ³Ό 값을 보면 μžμ‹ μ„ 감싼 μ›μ‹œκ°’μ˜ νƒ€μž…μ΄ μ•„λ‹ˆλΌ object의 ν•˜μœ„ νƒ€μž…μ— 가깝닀.

String {"string object"}
0: "s"
1: "t"
2: "r"
3: "i"
4: "n"
5: "g"
6: " "
7: "o"
8: "b"
9: "j"
10: "e"
11: "c"
12: "t"
length: 13
__proto__: String
[[PrimitiveValue]]: "string object"

console.log둜 ν™•μΈν–ˆμ„ λ•Œ λͺ¨μŠ΅ (λΈŒλΌμš°μ € λ§ˆλ‹€ λ‹€λ₯΄λ‹€)

String, Number, Boolean 같은 λ‹¨μˆœ primitiveλŠ” boxing 고정을 κ±°μΉœλ‹€.

Object.prototype.toString.call(stringPrimitive) // "[object String]"

String 으둜 ν‘œμ‹œλœ 것을 보아 ν•΄λ‹Ή 객체 래퍼둜 μžλ™ 박싱됨을 μ•Œ 수 μžˆλ‹€.

Wrapper Boxing

μ›μ‹œκ°’μ—” ν”„λ‘œνΌν‹°λ‚˜ λ©”μ„œλ“œκ°€ μ—†μœΌλ―€λ‘œ μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” μ›μ‹œ 값을 객체 레퍼둜 κ°μ‹Έμ•Όν•˜λŠ”λ° -> 이λ₯Ό μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μžλ™μ μœΌλ‘œ ν•΄μ€€λ‹€.

κ·Έλ ‡λ‹€λ©΄?

const str = "string";
for(let i = 0; i < str.length; i++) {
  console.log(str.charAt(i));
}

str.lengthλŠ” 객체둜 κ°μ‹ΈλŠ” 일이 μƒκΈ°λ‹ˆκΉŒ μ²˜μŒλΆ€ν„° 객체λ₯Ό 생성할 수 없도둝 String Object둜 μ“°λŠ”κ²Œ 더 μ’‹μ„κΉŒ?

닡은 놉!

  1. λΈŒλΌμš°μ €κ°€ 슀슀둜 μ΅œμ ν™” ν•œλ‹€.

  2. κ°œλ°œμžκ°€ μ„  μ΅œμ ν™”(pre-Opimize)ν•˜κ²Œ λœλ‹€λ©΄ 였히렀 더 느렀질 수 μžˆλ‹€.

직접 객체 ν˜•νƒœλ‘œ 써야할 μ΄μœ λŠ” 거의 μ—†λ‹€. μ•Œμ•„λ³΄κΈ° μ‰½κ²Œ μ›μ‹œ 값을 μ‚¬μš©ν•˜μž

μˆ˜λ™μœΌλ‘œ μ›μ‹œ 값을 λ°•μ‹±ν•˜κ³ μž ν•œλ‹€λ©΄ Object()ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μž

const stringPrimitive = "string primitve";
const stringObject = new String('string object');
const objSting = Object("string");

typeof stringPrimitive // "string"
typeof stringObject // "object"
typeof objSting // "object"

stringPrimitive instanceof String // false
stringObject instanceof String // true
objSting instanceof String // true

Object.prototype.toString.call(stringObject) // "[object String]"
Object.prototype.toString.call(objSting) // "[object String]"

객체 래퍼둜 직접 λ°•μ‹±ν•˜λŠ”κ±΄ κΆŒν•˜μ§€ μ•ŠλŠ”λ‹€.

ν•˜μ§€λ§Œ λ§Œμ•½ new Boolean(false 같은 경우 객체 래퍼둜 truthy 값이 λœλ‹€. μ΄λŸ°κ²½μš°λ“±μ„ λŒ€λΉ„ν•˜μ—¬ Object()둜 ν•˜μŸˆ

μ°Έκ³ 

  • You Don't Know JS: νƒ€μž…κ³Ό 문법, μŠ€μ½”ν”„μ™€ ν΄λ‘œμ €

carriage return 이것이 무엇인가.. μ˜ˆμ „ νƒ€μžκΈ°λ₯Ό 예둜 λ“€μ–΄μ•Ό ν•  정도 line feed와 carriage return이 ν•¨κ»˜ν•΄μ•Ό λ‹€μŒμ€„ μ‹œμž‘λΆ€λΆ„μ—μ„œ 글을 μ“Έ 수 μžˆμ—ˆλ‹€. μ°Έκ³ 

form feed If you were programming for a 1980s-style printer, it would eject the paper and start a new page. You are virtually certain to never need it.

https://devdocs.io/javascript/global_objects/string
https://devdocs.io/javascript/global_objects/string
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
https://stackoverflow.com/questions/17256182/what-is-the-difference-between-string-primitives-and-string-objects-in-javascrip
https://maximdenisov.gitbooks.io/you-don-t-know-js/content/types_&_grammar/natives.html
https://stackoverflow.com/questions/12747722/what-is-the-difference-between-a-line-feed-and-a-carriage-return
https://stackoverflow.com/questions/4334370/escape-sequence-f-form-feed-what-exactly-is-it