ES5和ES6函数你不知道的区别【面试篇】

2020-03-26 00:00:00 函数 代码 调用 方法 复制

前言

JS 中函数是高等公民,但是function 和 class 的区别你真的清楚嘛?
本文从PolyFill 实现,再到性能分析,再复习哈基础篇的使用;
另外深圳前端求坑,有坑的大佬麻烦内推一下。

1. PolyFill

1.利用原生 js 撸一个简单的 class;
2.根据上面的用法知道 class 必须通过 new 调用,不能直接调用;

// 阻止直接()调用,直接在ES6运行Parent(),这是不允许的,ES6中抛出Class constructor Parent cannot be invoked without 'new'错误
function _classCallCheck(instance, Constructor{
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}

相关文章