我之前的相关问题:How do I intercept sort function within a JS proxy?
给定代理:
function sort(customSort) {
/* Write logs */
console.log("Intercepted sort")
return this.sort(customSort);
}
function get( target, prop, receiver ) {
if (prop === 'sort') {
/* How to capture custom sort?*/
return sort.bind(target);
}
console.log("Intercepted get")
return Reflect.get( target, prop, receiver );
}
var handler = {
get
};
var proxy = new Proxy( new Array(...[7,1,2,3,4,5]), handler );但现在我添加了一个自定义排序函数:
console.log(proxy.sort((a,b) => .... /* e.g., a-b */))我不明白如何捕获函数并将其传递给代理排序函数。我尝试过在get函数中捕获不同的参数,但找不到正确的参数。
发布于 2021-10-21 10:38:14
解决方案类似于:How to sort only part of array? between given indexes
我返回一个访问argument[0]的函数
function sort(customSort) {
/* Write logs */
console.log("Intercepted sort")
return this.sort(customSort);
}
function get( target, prop, receiver ) {
if (prop === 'sort') {
/* How to capture custom sort?*/
return function(customSort) => {
return this.sort(customSort || ((a,b) => a-b) )
}
}
console.log("Intercepted get")
return Reflect.get( target, prop, receiver );
}https://stackoverflow.com/questions/69657122
复制相似问题