상세 컨텐츠

본문 제목

[자바스크립트] 빅인트를 계산하는방법

카테고리 없음

by esoesmio 2023. 5. 12. 21:24

본문

// 양쪽 모두 BigInt로 변환하여 계산하는 방법 사용
const calcAsBigInt = (x, y, op) => {
    return op(BigInt(x), BigInt(y));
}

console.log(
    calcAsBigInt(1n, 1, (x, y) => x + y)
);

 

 

 

심볼 프로퍼티를 가져오는법

 

const buildingKey = Symbol('secret');

const building = {
    name: '얄코사옥',
    floors: 3,
    [buildingKey]: '1234#'
}

console.log(building);

console.log(
    building.name,
    building.floors,
    building[buildingKey]
);

// 외부로부터의 접근 차단
console.log(
    building[Symbol('secret')]
);

 

 

심볼 레지스트리의 심볼이 쓰이는곳

 

// 숫자 요소들의 평균을 구하는 메서드 추가
Array.prototype[Symbol.for('average')] = function () {
  let sum = 0, count = 0;
  for (const i of this) {
    if (typeof i !== 'number') continue;
    count++;
    sum += i;
  }
  return sum/count
}

[1, 2, 3, 4, 5, 6][Symbol.for('average')]();

댓글 영역