실험 1 — 불변성은 진짜 차단되는가 (코드를 수정해서 실행해보세요)
코드
Run
"use strict"; const str = "hello"; try { str[0] = "H"; console.log("문자열 수정:", str); } catch (e) { console.log("문자열:", e.message); } const arr = ["h","e","l","l","o"]; arr[0] = "H"; console.log("배열 수정:", arr.join(""));
실험 2 — === 비교: 같은 내용, 다른 결과 (코드를 수정해서 실행해보세요)
코드
Run
const p1 = { name: "Lee" }; const p2 = { name: "Lee" }; console.log("p1 === p2 :", p1 === p2); console.log("p1.name===p2.name:", p1.name === p2.name); // 왜? console.log(""); console.log("typeof p1 :", typeof p1); console.log("typeof p1.name :", typeof p1.name);
실험 3 — 문자열 인터닝 (node --allow-natives-syntax 실행 결과)
node --allow-natives-syntax examples/ch11/02-interning-compact.js
=== 문자열 인터닝 실험 ===
리터럴 'hello' :
true
같은 리터럴 (변수2) :
true
'hel' + 'lo' :
false
'hello world'.slice :
false
JSON.parse :
true
=== 그런데 === 비교는? ===
literal1 === dynamic :
true
literal1 === sliced :
true
literal1 === parsed :
true
%IsInternalizedString()은 V8 내부 함수라 브라우저에서 실행할 수 없습니다.
위 결과는 Node.js에서 실제 실행한 출력입니다. 인터닝 여부가 달라도 === 비교는 항상 정확합니다.