[
'push',
'pop',
'shift',
'unshift',
'splice',
'sort',
'reverse'
].forEach(method => {
const original = ArrayProto[method];
Object.defineProperty(arrayMethods, method, {
value: function mutator(...args) {
console.log('mutator:', this, args);
return original.apply(this, args); //这里的this指向的是调用push的那个元素 为什么?
},
enumerable: false,
writable: true,
configurable: true
})
})
注释中的this指向了调用push的那个元素 为什么?
虽然MDN里的详细指南没有Object.defineProperty的value说明
但是这里有getter 与 setter 中的 this的指南
明确写到:
应该也是从了这条吧,所以指向的是调用的那个object
参考:MDN