我已经在javascript上下文中阅读了很多有关此的内容,并试图理解装饰器代码。每当我查看装饰器代码时,例如下面的代码,即使输入函数未对“ this”进行任何引用,它也始终将此输入函数应用于“ this”。这是为什么?是否有必要始终在装饰器中将函数应用于“ this”?它还指出,在许多地方,由于与装饰器的绑定,装饰器不能成为箭头功能。有人可以为什么会影响功能?
function doSomething(name) {
console.log('Hello,' + name);
}
function loggingDecorator(wrapped) {
return function() {
console.log('Starting');
const result = wrapped.apply(this,arguments);
console.log('Finished');
return result;
}
}
const wrapped = loggingDecorator(doSomething);