// 手写es6中数组的includes方法
Array.prototype.includes = function (item, index) {
var arr = this;
if(!item) return false;
if(index) {
return arr.indexOf(item, index) != -1;
}else {
return arr.indexOf(item) != -1;
}
}
// 手写new函数的实现
function _new(Func, ...args) {
// 1.创建一个FUNC的实例对象(实例.__proto__=>类.prototype)
// 在IE浏览器中,禁止我们使用__proto__(也可以理解为IE并没有提供给我们__proto__这个属性,防止我们去改变原型指向)
/* let obj = {};
obj.__proto__ = Func.prototype; //即将废弃该属性
Object.setPrototypeOf(obj, Fn.prototype); //兼容到IE11
*/
let obj = Object.create(Func.prototype);
// 2.把FUNC当做普通函数执行(让方法中的THIS指向创建的实例)
let result = Func.call(obj, ...args);
// 3.分析函数执行的返回值(没有返回值或者返回的是原始值类型则默认都返回创建的实例,否则以函数自身返回的为主)
if (result !== null && /^(object|function)$/.test(typeof result)) {
return result;
}
return obj;
}