es6-promise
demo:
<script>
window.Promise = 32;
</script>
<script src="https://cdn.bootcss.com/es6-promise/4.1.1/es6-promise.js"></script>
<script>
console.log(Promise); // 32, 說明沒有自動polyfill
</script>
demo:
<script>
window.Promise = 32;
</script>
<script src="https://cdn.bootcss.com/es6-promise/4.1.1/es6-promise.js"></script>
<script>
//此處在瀏覽器中為window.Promise = ES6Promise.Promise;
//CommonJS中則為 window.Promise = require('es6-promise').Promise;
window.Promise = ES6Promise.Promise;// 與執(zhí)行ES6Promise.polyfill(); 的效果完全一樣,ES6Promise.polyfill()會覆蓋掉原生的Promise對象广鳍;
console.log(Promise);// ? Promise$2(resolver) {}, okay了
</script>
es6-promise.auto
demo:
es6-promise.auto會檢測Promise是否存在销凑,若存在原生的Promise,則不執(zhí)行每币;
否則會應(yīng)用polyfill;
應(yīng)用:
<script>
window.Promise = 32;
</script>
<script src="https://cdn.bootcss.com/es6-promise/4.1.1/es6-promise.auto.js"></script>
<script>
console.log(Promise);//? Promise$3(resolver) {} ,已被更改
</script>
不應(yīng)用:
<script>
// window.Promise = 32;
</script>
<script src="https://cdn.bootcss.com/es6-promise/4.1.1/es6-promise.auto.js"></script>
<script>
console.log(Promise); // ? Promise() { [native code] }, 未被更改
</script>
總結(jié)
也就是說磁滚,es6-promise是個庫而已,不會自動polyfill;
要想自動polyfill那么就用es6-promise.auto宵晚。