1、Reflect.getPrototypeOf() 方法与 Object.getPrototypeOf() 方法 都要求传入的参数为对象。 如果传入的参数不是对象,则
Reflect.getPrototypeOf() 方法会抛出错误,而 Object.getPrototypeOf() 方法则会在执行前将参数强制转换为一个对象。
Reflect.setPrototypeOf() 方法与Object.setPrototypeOf() 方法也不尽相同。 Reflect.setPrototypeOf() 方法返回一个布尔值来表示
操作是否成功,成功时返回true ,失败时返回false;Object.setPrototypeOf() 方法一旦失败就会抛出一个错误。
2.es5代码
function weapon(weapon){
this.weapon = weapon;
}
Warrior.prototype.wield = function () {
return "Wielding" + this.weapon;
};
Warrior.duel = function (warrior1, warrior2) {
return warrior1.wield() + " " + warrior2.wield();
};
转化为es6代码
class Warrior {
constructor(weapon) {
this.weapon = weapon;
}
wield() {
return "Wielding " + this.weapon;
}
static duel(warrior1, warrior2) {
return warrior1.wield() + " " + warrior2.wield();
}
}
//需要评估isPrime函数的性能,并且不能修改函数的代码,使用代理包装函数,添加一旦调用该函数就会除法的方法:apply 方法
//每当调用isPrime函数都会进入代理的apply方法,开启内置的console.time方法秒表计时, 调用原始的isPrime函数
//记录运行时间,最后返回isPrime调用的结果。
使用代理自动填充属性,当访问未定义的属性时,并不会报错,会自动填充不存在于对象上的属性。
使用代理实现负向数组索引:
简化版的代理模拟负向数组索引,可以访问值和设置值;访问最后一个数组元素,使用array[-1] 比使用 array[array.length - 1] 更加简便。