简体   繁体   中英

`String.prototype.at` is not implemented in all browsers. Is there a @babel/plugin that will transform this down to ES5 javascript?

At the time of this question String.prototype.at is not implemented in all browsers.

I would like to use the at function on the String prototype within a React app that uses babel to transform modern ecmascript into older more browser compatible versions of javascript.

I have been unsuccessful in finding a babel plugin that would do this for me.

Is there a @babel/plugin that will transform the String prototype at function down to more browser compatible ES5 javascript?

String.prototype.at is equal this function

let text = "hello, how are you";
function at(a, b) {
    return a.charAt(b < 0 ? a.length-Math.abs(b) : b);
}
console.log(at(text, -3));
console.log(text.at(-3));

Babel is for syntax-level transformations, not browser API polyfills.

You can consider using a polyfill service like Polyfill.io , or alternatively, simply use the polyfill code from the relevant proposal .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM