JavaScript手写代码
1. 实现一个new操作符
new操作符function New(func) {
// 创建一个全新的对象
var res = {};
if (func.prototype !== null) {
// 通过new创建的每个对象将链接到这个函数的prototype对象上
res.__proto__ = func.prototype;
}
// 使this指向新创建的对象
var ret = func.apply(res, Array.prototype.slice.call(arguments, 1));
if ((typeof ret === "object" || typeof ret === "function") && ret !== null) {
// 返回该对象引用
return ret;
}
return res;
}
var obj = New(A, 1, 2);
// equals to
var obj = new A(1, 2);2. 实现一个JSON.stringify
JSON.stringify3. 实现一个JSON.parse
JSON.parse3.1 第一种:直接调用 eval
eval3.2 第二种:Function
4. 实现一个call或apply
call或apply4.1 Function.call按套路实现
Function.call按套路实现4.2 Function.apply的模拟实现
Function.apply的模拟实现5. 实现一个Function.bind()
Function.bind()6. 实现一个继承
7. 实现一个 JS 函数柯里化
7.1 通用版
7.2 ES6写法
ES6写法8. 手写一个Promise
Promise8.1 Promise流程图分析
Promise流程图分析8.2 手写
8.3 大厂专供版
9 防抖 (Debounce) 和 节流 (Throttle)
Debounce) 和 节流 (Throttle)9.1 防抖 (Debounce) 实现
Debounce) 实现9.2 节流 (Throttle) 实现
Throttle) 实现10. 手写一个深拷贝
10.1 乞丐版
10.2 网传圣杯模式
简约版实现
11. 实现一个instanceOf
instanceOf12. 数组去重
13. 将浮点数点左边的数每三位添加一个逗号
如何判断一个对象是否属于某个类?
最后更新于