V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
serge001

babel-polyfill 是如何实现在 IE 把 Array.from polyfill 为 native code 的?

  •  
  •   serge001 · Mar 22, 2020 · 2992 views
    This topic created in 2228 days ago, the information mentioned may be changed or developed.

    使用 vue-cli3 的脚手架,默认配置了 polyfill 了.我在 index.html 中手动重置 Array.prototype.includes 为 undefined, 然后在组件中使用 Array.includes 方法,然后在控制台中打印 Array.prototype.includes,打印出来的结果均为 fuction () {native code}

    这让我觉得有点不可思议,对于 chrome 来说,因为 includes 方法本身就是 native support 的,我重置后,babel 还可以通过 iframe 来恢复,可是对于 ie 来说,本来就没有对 includes 方法支持,按道理打印出来的应该是 polyfill 的代码而不是 native code,babel 怎么实现这种黑魔法的?

    4 replies    2020-03-22 03:18:52 +08:00
    serge001
        1
    serge001  
    OP
       Mar 22, 2020 via Android
    可以打开这个链接在 IE 中测试 https://gaoshijun1993.github.io/
    randyo
        2
    randyo  
       Mar 22, 2020 via Android
    应该不是吧,打断点都能进去
    Kokororin
        3
    Kokororin  
       Mar 22, 2020
    autoxbc
        4
    autoxbc  
       Mar 22, 2020
    const fn = () => 42 ;
    fn.toString = () => 'function() { [native code] }';

    console.log(fn);

    >>'function() { [native code] }'
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5571 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 08:40 · PVG 16:40 · LAX 01:40 · JFK 04:40
    ♥ Do have faith in what you're doing.