Polyfill

dana·2021년 11월 7일
0

CS

목록 보기
1/11
post-thumbnail

Polyfill이란?

A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.
낮은 버전의 브라우저에서 높은 버전의 기능을 제공하기 위해 사용되는 일련의 코드

polyfills & transpilers

transpiler는 소프트웨어의 일부로 소스코드간 변환하는 기능을 갖고 있다.
최근 코드를 '읽고 해석하고' 예전 문법으로 재작성해준다.

그러나 엄청 오래된 자바스크립트 엔진은 새로운 기능에 대한 transpiler를 갖고 있지 않는 경우가 있는데, 이 때 새 기능을 추가해 주는 코드를 polyfill 이라고 한다.

예를 들어 Math.trunc 라는 내장함수를 갖고 있지 않은 오래된 버전의 자바스크립트 엔진이라면 기존에 엔진이 갖고 있는 Math.ceil()과 Math.floor()를 이용해 이를 호환할 수 있는 코드를 작성해주면 그게 polyfill이 된다.

if (!Math.trunc) { // if no such function
  // implement it
  Math.trunc = function(number) {
    // Math.ceil and Math.floor exist even in ancient JavaScript engines
    // they are covered later in the tutorial
    return number < 0 ? Math.ceil(number) : Math.floor(number);
  };
}

https://javascript.info/polyfills

profile
PRE-FE에서 PRO-FE로🚀🪐!

0개의 댓글