const str = "Hello MALZA";
console.log(str.replaceAll("l", "*")); // He**o MALZA
// 대 소문자 구분하는 거 까지 확인 된다.

// 기존 replace 는 처음 만나는 부분
const str = "Hello MALZA";
console.log(str.replace(/l/, "*")); // He*lo MALZA
// 또 g 를 붙여줌으로써 전부 다 바꿔주는 방식
console.log(str.replace(/l/g, "*")); // He**o MALZA
// promise.any

const resPromise = new Promise((res, rej) => {
  setTimeout(() => {
    res("success");
  }, 2000);
});

const rejPromise = new Promise((res, rej) => {
  setTimeout(() => {
    rej("fail");
  }, 1000);
});

Promise.race([rejPromise, resPromise])
  .then(() => console.log("성공"))
  .catch((e) => console.log(e));

Promise.any([rejPromise, resPromise])
  .then(() => console.log("성공"))
  .catch((e) => console.log(e));

// (1초 후) 실패
// (2초 후) 성공

// race 는 가장 먼저 완료된 결과값으로 이행 또는 거부
// any 는 가장 먼저 성공한 객체를 반환. 모두 거부라면 catch 실행
// nullish coalescing operator
function add(num1, num2) {
  num1 ||= 0;
  num2 ||= 0;
  console.log(num1 + num2);
}

let name = "malza";

name &&= `hello ${name}`;
console.log(name);
=====================================
// name 이 null 이거나 undefined 일 때 malza 가 값이 된다.
let name;

name ??= "malza";
console.log(name); // malza. name 에 어떤 값을 주면 그 값으로 출력된다.
// numeric separators
let billion = 1_000_000_000; // _ 로 값을 표기 가능하다.
console.log(billion);
// weakRef
// 가능한 피하라고 가이드 되어있다.
// 개발자가 참조 하고 있는 메모리는 가비지 컬렉터가 지우지 않는다.
// 그렇지만 약함 참조는 가비지 컬레터의 대상이 된다.
let user = { name: "malza", age: 30 };
const weakUser = new WeakRef(user);

user = null; // timer 시작 직전 user 를 null 로 바꾸고 참조를 끊어준다.

const timer = setInterval(() => {
  const wUser = weakUser.deref(); // 참조에 접근
  if (wUser) {
    console.log(wUser.name);
  } else {
    console.log("제거");
    clearInterval(timer);
  }
}, 1000);
// 8 malza -> 8번 호출되었다.
// 제거 -> 제거 되었다.

// 약한 참조로 일정 시간만 참조한 것이다.
// 가비지가 되어서도 가비지 콜렉터의 선택을 받지 못하는 string 참조를 방지 할 수 있다.