Javascript ๊ณต๋ถ€

2. JS์˜ ๋‹ค์–‘ํ•œ ๊ฐ’ ๋น„๊ต์™€ ์–•์€ ๋น„๊ต, ๊นŠ์€ ๋น„๊ต

tnqlscho 95 2024. 7. 8. 02:27

๐Ÿ‘€  ๋ชฉํ‘œ

1.์›์‹œํƒ€์ž…๊ณผ ์ฐธ์กฐํƒ€์ž…์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ธฐ

2.๋ฆฌ์•กํŠธ์—์„œ ์–ด๋–ค์‹์œผ๋กœ ๊ฐ’๊ณผ ์ฐธ์กฐ๊ฐ€ ์“ฐ์ด๋Š”์ง€ ์ดํ•ดํ•˜๊ธฐ

3.๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์žฌ์ ์†Œ์— ์ž˜ ์‚ฌ์šฉํ•˜๊ธฐ

 

๐Ÿ‘€  ์–•์€ ๋น„๊ต๋กœ ์‚ดํŽด๋ณด๋Š” ๋‹ค์–‘ํ•œ ๊ฐ’ ๋น„๊ต

๐ŸŸฃ ๋ฆฌ์•กํŠธ์—์„œ์˜ ์–•์€ ๋น„๊ต

์–•์€ ๋น„๊ต๋Š” ๊ฐ์ฒด์˜ ์ฐธ์กฐ์ฃผ์†Œ๋งŒ ๋˜‘๊ฐ™์€์ง€ ๋น„๊ตํ•œ๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ ์–•์€ ๋น„๊ต๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์ƒํƒœ๊ฐ€ ๋ณ€ํ• ๋•Œ ํŽ˜์ด์ง€๊ฐ€ ๋ Œ๋”๋ง ๋˜๋ฉด ๋‹ค๋ฅธ ์ƒํƒœ๊ฐ’๋“ค์ด ์ผ์น˜ํ•˜๋Š”์ง€ ์ฒดํฌํ•˜๋Š”๋ฐ

๊นŠ์€ ๋น„๊ต๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ์ชฝ์˜ depth๊นŒ์ง€ ๋˜‘๊ฐ™์€์ง€ ๋‘˜๋Ÿฌ๋ณด๊ณ  ์˜ค๊ธฐ๋•Œ๋ฌธ์— ์—ฐ์‚ฐ๋น„์šฉ์ด ๋” ๋งŽ์ด๋“ ๋‹ค.

๊ทธ๋ž˜์„œ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฆฌ์•กํŠธ์—์„œ ๋ Œ๋”๋งํ• ๋•Œ ์–•์€ ๋น„๊ต๋ฅผ ์‚ฌ์šฉํ•ด์„œ state์˜ ์ฐธ์กฐ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด

๊ฐ’์ด ๋ฐ”๋€Œ์—ˆ๊ตฌ๋‚˜! ์ธ์ง€ํ•˜๊ณ  ์žฌ ๋ Œ๋”๋ง ํ•˜๊ฒŒ๋œ๋‹ค.

 

๊ทธ๋ž˜์„œ useState์— ๊ฐ’์„ ๋ณ€๊ฒฝํ• ๋•Œ ๋ถˆ๋ณ€์„ฑ์„ ์ง€์ผœ์ฃผ๊ธฐ ์œ„ํ•ด ...์„ ์“ฐ๊ฑฐ๋‚˜ a.concat(atherArr)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

๐ŸŸฃ ๋‹ค์–‘ํ•œ ๊ฐ’ ๋น„๊ต

export function shallowEquals(target1, target2) {
  // ์›์‹œ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋น„๊ต
  if (Object.is(target1, target2)) return true;

  // ํƒ€์ž…์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ
  if (typeof target1 !== typeof target2) return false;

  // ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
  if (typeof target1 !== 'object' || target1 === null || target2 === null) {
    return false;
  }

  // ์ต๋ช… class ์กฐ๊ฑด ๋ฐ new ์ƒ์„ฑ์ž ๊ฐ์ฒด ์กฐ๊ฑด
  if ((!target1.constructor.name || !target2.constructor.name) ||
      (target1 instanceof Number || target2 instanceof Number) ||
      (target1 instanceof String || target2 instanceof String)) {
    return false;
  }

  // ๋ฐฐ์—ด ๋น„๊ต
  if (Array.isArray(target1) && Array.isArray(target2)) {
    if (target1.length !== target2.length) return false;
    for (let i = 0; i < target1.length; i++) {
      if (!Object.is(target1[i], target2[i])) return false;
    }
    return true;
  }

  // ๊ฐ์ฒด ํ‚ค ๋น„๊ต
  const target1Keys = Object.keys(target1);
  const target2Keys = Object.keys(target2);

  if (target1Keys.length !== target2Keys.length) return false;

  for (let i = 0; i < target1Keys.length; i++) {
    const currentKey = target1Keys[i];
    if (!Object.hasOwnProperty.call(target2, currentKey) || !Object.is(target1[currentKey], target2[currentKey])) {
      return false;
    }
  }
  return true;
}

 

1. Object.is()

๋‘ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•œ๋‹ค.

์›์‹œํƒ€์ž… ๊ฐ์ฒด ํƒ€์ž… ๋ชจ๋‘ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.

=== ์—ฐ์‚ฐ์ž๋ณด๋‹ค ๋” ์—„๊ฒฉํ•˜๊ฒŒ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

1. NaN์„ NaN๊ณผ ๊ฐ™๋‹ค๊ณ  ๋ด„
2. -0๊ณผ +0์€ ๋‹ค๋ฅด๊ฒŒ ๋ณธ๋‹ค.

 

console.log(Object.is('foo', 'foo')); // true
console.log(Object.is(window, window)); // true
console.log(Object.is('foo', 'bar')); // false
console.log(Object.is([], [])); // false
console.log(Object.is(null, null)); // true
console.log(Object.is(0, -0)); // false
console.log(Object.is(-0, -0)); // true
console.log(Object.is(NaN, 0/0)); // true

console.log(NaN === NaN); // false
console.log(0 === -0); // true

 

2. Array.isArray

๋ฌด์กฐ๊ฑด ๋ฐฐ์—ด์ธ์ง€๋งŒ ๋น„๊ตํ•œ๋‹ค.

๋ฐฐ์—ด์ƒ์„ฑ์ž์ธ new Array()๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฐฐ์—ด๋„ js์—์„œ๋Š” ๋ฐฐ์—ด ๊ฐ์ฒด๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Array.isArray([]); // true
Array.isArray(document.querySelectorAll('div')); // false

 

3. Object.keys()

๊ฐ์ฒด์˜ ํ‚ค๋“ค์„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์—ด๊ฑฐ๋ถˆ๊ฐ€๋Šฅํ•œ ์†์„ฑ์€ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. (enumerable: false)

๋ฐ˜๋Œ€๋กœ Object.value()๋Š” ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // ['a', 'b', 'c']

 

4. Object.hasOwnProperty.call(this, value)

๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์—์„œ ์ง์ ‘ ์ƒ์†๋œ ์†์„ฑ์„ ๋ฌด์‹œํ•˜๊ณ , ํ•ด๋‹น ๊ฐ์ฒด ์ž์ฒด์˜ ์†์„ฑ๋งŒ์„ ํ™•์ธํ•œ๋‹ค.

๊ฐ์ฒด์˜ ์†์„ฑ์„ ์•ˆ์ „ํ•˜๊ฒŒ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

๋’ค์— call์„ ๋ถ™์ด๋ฉด this๋ฅผ ์›ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

const parent = { a: 1 };
const child = Object.create(parent);
child.b = 2;

// child ๊ฐ์ฒด ์ž์ฒด์˜ ์†์„ฑ๋“ค์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
console.log(Object.keys(child)); // ['b']

// parent ๊ฐ์ฒด์˜ ์†์„ฑ์„ ํฌํ•จํ•œ ๋ชจ๋“  ์†์„ฑ๋“ค์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
for (let key in child) {
  console.log(key); // 'b', 'a'
}

// child ๊ฐ์ฒด์˜ ์†์„ฑ ์ค‘์—์„œ ์ง์ ‘์ ์œผ๋กœ child์— ์†ํ•œ ์†์„ฑ์ธ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
console.log(child.hasOwnProperty('b')); // true
console.log(child.hasOwnProperty('a')); // false

// Object.hasOwnProperty.call์„ ์‚ฌ์šฉํ•˜์—ฌ child ๊ฐ์ฒด์˜ ์ง์ ‘์ ์ธ ์†์„ฑ์ธ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
console.log(Object.hasOwnProperty.call(child, 'b')); // true
console.log(Object.hasOwnProperty.call(child, 'a')); // false
console.log(Object.hasOwnProperty.call(parent, 'b')); // false

 

5. ์ต๋ช… class ๋งž๋Š”์ง€ ๋น„๊ต

์•„๋ž˜์˜ class๋Š” ๋ณ€์ˆ˜๋ช…์€ ์žˆ์ง€๋งŒ class ์ด๋ฆ„์€ ์—†๋Š” ์ต๋ช… class๋‹ค.

์ต๋ช… ํด๋ž˜์Šค๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์ด๋ฆ„์ด ์—†๊ธฐ๋•Œ๋ฌธ์— constructor.name ํ•˜๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ์ต๋ช… ํด๋ž˜์Šค์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

const MyClass = class {
  constructor() {
    // constructor ๋‚ด์šฉ
  }
};

 

6. ๊ฐ์ฒด๊ฐ€ ๋งž๋Š”์ง€ ๋น„๊ต

js๋Š” ์œ ๋ช…ํ•˜๊ณ  ๋…ํŠนํ•œ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

null์˜ type๋„ 'object'์ธ๊ฒƒ.

๊ทธ๋ž˜์„œ ๊ฐ์ฒด๊ฐ€ ๋งž๋Š”์ง€๋ฅผ ๋น„๊ตํ• ๋•Œ๋Š” null์ด ์•„๋‹Œ์ง€๋„ ํ•œ๋ฒˆ ๋” ์กฐ๊ฑด์„ ๊ฑธ์–ด์ค˜์•ผ ํ•œ๋‹ค.

if (typeof target1 !== 'object' || target1 === null || target2 === null) {
    return false;
}

 

 

๐Ÿ‘€  ๊นŠ์€ ๋น„๊ต์™€ ์žฌ๊ท€

๊นŠ์€ ๋น„๊ต๋Š” ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ• ๋•Œ ์•ˆ์ชฝ์˜ depth์˜ ๊ฐ์ฒด์˜ ๊ฐ’๊นŒ์ง€ ์™„์ „ ๋˜‘๊ฐ™์€์ง€ ๋น„๊ตํ•œ๋‹ค.

์•ˆ์ชฝ์˜ depth๊นŒ์ง€ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

export function deepEquals(target1, target2) {
  // shallowEquals์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ์žฌ๊ท€์ ์œผ๋กœ ๋‚ด๋ถ€ ๊ฐ์ฒด๊นŒ์ง€ ๋น„๊ต

  // ๋‘ ๊ฐ’์ด ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ฒฝ์šฐ true ๋ฐ˜ํ™˜
  if (target1 === target2) {
    return true;
  }

  // target1๊ณผ target2๊ฐ€ ๋ชจ๋‘ ๋ฐฐ์—ด์ด๊ณ , ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ ๊ฐ ์š”์†Œ๋ฅผ ๋น„๊ต
  if (Array.isArray(target1) && Array.isArray(target2) && target1.length === target2.length) {
    return target1.every((v, k) => deepEquals(v, target2[k]));
  }

  // target1๊ณผ target2๊ฐ€ ๋ชจ๋‘ ๊ฐ์ฒด์ด๊ณ , ๊ฐ ์†์„ฑ๋“ค์„ ๋น„๊ต
  if (target1.constructor === Object && target2.constructor === Object) {
    const key1 = Object.keys(target1); // target1์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    const key2 = Object.keys(target2); // target2์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    
    // ๊ฐ์ฒด ์ž์ฒด๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ (๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜)
    // ์†์„ฑ์˜ ์ˆ˜๊ฐ€ ๊ฐ™๊ณ  ๋ชจ๋“  ํ‚ค์— ๋Œ€ํ•ด ์žฌ๊ท€์ ์œผ๋กœ deepEquals๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ’ ๋น„๊ต
    return target1 === target2 || (key1.length === key2.length && key1.every(key => deepEquals(target1[key], target2[key])));
  }

  // ์œ„ ์กฐ๊ฑด์— ๋ชจ๋‘ ํ•ด๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ๋‘ ๊ฐ’์€ ๊ฐ™์ง€ ์•Š์Œ
  return false;
}

 

 

๐Ÿ‘€   new Number๊ณผ  newString

expect(num1 == 1).toBe(true);

expect(typeof num1 === 'number').toBe(false);

expect(typeof num1 === 'object').toBe(true);

 

expect(num1 == '1').toBe(true);

expect(typeof num1 === 'string').toBe(false);

expect(typeof num1 === 'object').toBe(true);

 

num1์ด ๊ฐ’์€ ๊ฐ™์€๋ฐ ์—„๊ฒฉํ•œ ๋น„๊ต๋ฅผ ํ•˜๋ฉด ์ˆซ์ž๋‚˜ ๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๊ณ  ํ•œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ type์€ object์ด๋‹ค.

/** typeof๊ฐ€ object์ธ๊ฒŒ true๋‹ˆ๊นŒ ๋“ค์–ด์˜ค๋Š” ๊ฐ’์„ new Number๋กœ type์„ object๋กœ ๋งŒ๋“ค๊ธฐ */
export function createNumber1(n) {
  return new Number(n); // [Number: 1]
}

/** string์ฒ˜๋Ÿผ ๋ถ™๋Š”๋ฐ, type์ด object. */
export function createNumber2(n) {
  return new String(n); //[String: '1']
}

 

์œ„์˜ ์กฐ๊ฑด๋“ค์„ ๋ชจ๋‘ ์ถฉ์กฑํ•˜๋Š”๊ฑด new ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“  Number ๊ฐ์ฒด๊ณผ String ๊ฐ์ฒด์ด๋‹ค.

๊ทธ๋ž˜์„œ ์›์‹œํƒ€์ž…์ด ๋งž๋ƒ๊ณ  ๋ฌผ์–ด๋ณด๋ฉด false๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

๐Ÿ‘€   valueOf, toJSON, toString

/** ๊ฐ์ฒด๊ฐ€ ์ˆซ์ž์ปจํ…์ŠคํŠธ(๊ฐ์ฒด๊ฐ€ ์ˆซ์ž๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ)์—์„œ ์‚ฌ์šฉ๋  ๋•Œ ๊ฐ์ฒด์— valueOf ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด
 * 'valueOf'๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜์–ด ๋ฉ”์„œ๋“œ์˜ return ๊ฐ’์œผ๋กœ ์—ฐ์‚ฐ.
 * ์ˆซ์ž์ปจํ…์ŠคํŠธ ๋ฐœ์ƒ : ์ˆ˜ํ•™ ์—ฐ์‚ฐ, ๋น„๊ต ์—ฐ์‚ฐ, ๋‹จํ•ญ ์—ฐ์ƒ
 * JSON.stringify๋กœ ๋ฌธ์ž์—ด ๋ณ€ํ™˜์„ ์‹œ๋„ํ•˜๋ฉด toJSON ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ ,
 * ๋ฌธ์ž์—ด ์ปจํ…์ŠคํŠธ์—์„œ๋Š” toString ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
**/
export function createNumber3(n) {
  return {
    value: n,
    valueOf: function() {
        return this.value;
    },
    toJSON: function() {
        return `this is createNumber3 => ${this.value}`;
    },
    toString: function() {
        return `${this.value}`;
    }
  };
}

 

 

๐Ÿ‘€   new Map()

๊ฐ™์€ ์ฐธ์กฐ๋ฅผ ๋ฐ”๋ผ๋ด์•ผ ํ•˜๋Š” ์กฐ๊ฑด์„ ์œ„ํ•ด new Map์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์บ์‹ฑํ•œ๋‹ค.

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ ์บ์‹œ๋ฅผ ํ™•์ธํ•ด์„œ ์ด์ „๊ณผ ๋™์ผํ•œ ๊ฐ์ฒด๊ฐ€ ์žˆ์„๋•Œ ๊ทธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋™์ผํ•œ ๊ฐ์ฒด๊ฐ€ ์—†์„๋•Œ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Map์— ์บ์‹ฑํ•œ๋‹ค.

export class CustomNumber {
  static cache = new Map();

  constructor(n){
    if (CustomNumber.cache.has(n)) {
      return CustomNumber.cache.get(n);
    }

    this.value = n;
    CustomNumber.cache.set(n, this);
  }

  valueOf() {
    return this.value;
  }

  toJSON(){
    return `${this.value}`;
  }

  toString(){
    return `${this.value}`;
  }
}

 

๐Ÿ‘€ Object.defineProperty 

js์˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ• ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ.

๋นˆ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ์ง€๋ฉด ์–•์€ ๋ณต์‚ฌ๋ฅผ ํ• ๋• ๋นˆ ๊ฐ์ฒด์™€ ๊ฐ™์•„์•ผ ํ• ๋•Œ Object.defineProperty๋กœ

์ง์ ‘ ์ ‘๊ทผํ• ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ๊ฐ ์†์„ฑ์„ ๋ฃจํ”„, ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ์—ด๊ฑฐ(๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ฐ˜๋ณตํ•ด์„œ ๋‚˜์—ด)ํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.

const obj = createUnenumerableObject({ a: 1, b: 2 });
it('createUnenumerableObject > ', () => {
  expect(obj.a).toEqual(1);
  expect(obj.b).toEqual(2);
  expect({ ...obj }).toEqual({});
})

 

๊ทธ๋Ÿผ ์œ„์˜ ๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’์„ ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ

๊ฐ์ฒด๋ฅผ ์–•์€๋ณต์‚ฌ๋ฅผ ํ•˜๋ฉด enumerable์€ false๋กœ ํ–ˆ๊ธฐ๋•Œ๋ฌธ์— ์—ด๊ฑฐ๊ฐ€๋Šฅํ•œ ์†์„ฑ์ด ์—†์–ด ๋นˆ ๊ฐ์ฒด๊ฐ€ ๋‚˜์˜จ๋‹ค.

export function createUnenumerableObject(target) {
  const result = {};
  Object.keys(target).forEach(key => {
    Object.defineProperty(result, key, {
      value: target[key],
      enumerable: false,
      writable: true,
      configurable: true
    });
  });
  return result;
}

 

7. Object.getOwnPropertyNames()

for ...in ๋ฃจํ”„๋‚˜ ์•ž์˜ Object.keys()๋Š” ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์†์„ฑ๋งŒ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ

Object.getOwnPropertyNames()๋Š” ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์†์„ฑ๊ณผ ๋น„์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์†์„ฑ ์ „๋ถ€ ์ด๋ฆ„์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const obj = Object.create({}, {
  a: { value: 1, enumerable: true }, // ์—ด๊ฑฐ ๊ฐ€๋Šฅ
  b: { value: 2, enumerable: false } // ์—ด๊ฑฐ ๋ถˆ๊ฐ€๋Šฅ
});

obj.c = 3; // ์—ด๊ฑฐ ๊ฐ€๋Šฅ

console.log(Object.getOwnPropertyNames(obj)); // ["a", "b", "c"]
console.log(Object.keys(obj)); // ["a", "c"]

 

8. Object.getPrototypeOf()

๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ฐ์ฒด๊ฐ€ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Object.create()๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ํ™•์ธํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

 

const obj = {};
const proto = Object.getPrototypeOf(obj);

console.log(proto === Object.prototype); // true

 


tmi...

์™€.. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ์ฒ˜์Œ๋ณด๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ดํ•ดํ•ด์•ผ ํ•˜๊ณ  ์ˆ˜๋งŽ์€ ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ํ†ต๊ณผํ•ด์•ผ ํ•˜๋Š” ๋กœ์ง์„ ์งœ์•ผํ•˜๋Š”๋ฐ

์ง„์งœ ๋จธ๋ฆฌ ์ฅ์–ด๋œฏ์–ด๋„ ๊ตฌ๊ธ€๋ง๊ณผ gpt๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ ˆ๋Œ€ ์ดํ•ดํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

๊ตฌ๊ธ€๋ง๊ณผ gpt๋ฅผ ๋Œ๋ ค๋ด๋„ ์ดํ•ด์•ˆ๋˜๋Š”๊ฒƒ๋„ ์žˆ๋‹ค๋Š”๊ฑด ํ•จ์ •..ใ… 

์ด๋ฒˆ์ฃผ์ฐจ๋Š” ๋‚ด ์ƒ๊ฐ์ด ๋“ค์–ด๊ฐ€์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ”ผ๋“œ๋ฐฑ์ด ์†Œ์šฉ์ด ์—†์—ˆ๋‹ค.. ๋‚˜ ์–ด๋–กํ•ด์•  ๐Ÿ˜‚