javascript - 如何创建将拦截 vanilla js 中的任何属性和函数调用的模拟对象?
问题描述
我想在 vanilla js 中创建对象,其行为如下:
var obj = MyObj()
obj.call.any.function() / should print something like "called MyObj with call.any.function()"
obj.some.other.method() / "called MyObj with some.other.method()"
我已经尝试过代理对象,但我只能做这样的事情:
class MyObj{
constructor(){
return new Proxy(this, {
get: function(target, prop) {
return function() {
console.log('function call', prop, arguments);
return;
};
}
});
}
}
仅适用于 obj.call() 之类的函数调用,但不适用于 obj.some.call()
解决方案
推荐阅读
- clojure - Clojure 调用一系列函数并存储它们的返回值
- django - 如何通过 id 在 Django 中加入两个模型?
- vue.js - 更改网址后立即停止观看
- ios - 如何在 SwiftUI 中使用图像 CDN?
- python - 如何在命令行上使用“python -c”缩进
- xcode - 由于 pod 链接错误,Flutter 归档构建失败
- postgresql - 如何从 C 列获取值,其中对应的 A 列值在 postgres 的 B 列中有不同的值?
- c# - 如何在不提取的情况下递归探索 zip 文件内容
- python - 如何在python中更改二维列表的顺序
- dicom - 应该使用什么软拷贝呈现状态?