Cod*_*iac 7 javascript object ecmascript-6 es6-proxy
TL:DR; 是否有可能使对象的属性仅是可调用的(作为函数)?
我的意思是
class Foo{
bar(value){
return value
}
}
let newFoo = new Foo()
console.log(newFoo.bar(123)) // should work fine as function is invoked
console.log(newFoo.bar) // here i need to throw or display an error instead of returning value
Run Code Online (Sandbox Code Playgroud)
我尝试使用Proxy
and handler.get
trap 做到这一点,但是我不知道如何捕获它是函数调用还是属性访问,
class Foo {
bar(value) {
return value
}
}
const proxied = new Proxy(new Foo(), {
get: function(target, prop, reciver) {
if (prop === 'bar') {
throw new Error('Bar is method need to be invoced')
}
return target[prop]
}
})
console.log(proxied.bar(true))
console.log(proxied.bar)
Run Code Online (Sandbox Code Playgroud)
我也检查了一下,handler.apply
但这似乎也没有用,因为这是函数的陷阱,而不是属性的陷阱
class Foo {
bar(value) {
return value
}
}
const proxied = new Proxy(new Foo(), {
apply: function(target, thisArg, argumentsList) {
return target(argumentsList[0])
},
get: function(target, prop, reciver) {
if (prop === 'bar') {
throw new Error('Bar is method need to be invoced')
}
return target[prop]
}
})
console.log(proxied.bar(true))
console.log(proxied.bar)
Run Code Online (Sandbox Code Playgroud)
不,这是不可能的。之间没有区别
const newFoo = new Foo()
newFoo.bar(123);
Run Code Online (Sandbox Code Playgroud)
和
const newFoo = new Foo()
const bar = newFoo.bar;
Function.prototype.call.call(bar, newFoo, 123); // like `bar.call(newFoo, 123)`
// or Reflect.apply(bar, newFoo, [123]);
Run Code Online (Sandbox Code Playgroud)
即既不能newFoo
也bar
不能“从内部”区分这些。现在,在属性访问和方法调用之间可能发生任意事情,并且在属性访问期间您无法知道接下来会发生什么,因此您不能过早抛出异常。方法调用可能永远不会发生(在newFoo.bar;
),并且无法newFoo
单独识别这一点。
唯一的方法是在检测到恶意序列后newFoo
拦截对其及其属性的所有其他访问;整个程序运行后,可能让你的“linter”从外部检查序列:throw
const lint = {
access: 0,
call: 0,
check() {
console.log(this.access == this.call
? "It's ok"
: this.access > this.call
? "method was not called"
: "property was reused");
},
run(fn) {
this.call = this.access = 0;
try {
fn();
} finally {
this.check();
}
}
}
Run Code Online (Sandbox Code Playgroud)
function bar(value) {
lint.call++; lint.check();
return value;
}
class Foo {
get bar() {
lint.check(); lint.access++;
return bar;
}
}
lint.run(() => {
const newFoo = new Foo;
newFoo.bar(123);
});
lint.run(() => {
const newFoo = new Foo;
newFoo.bar;
});
lint.run(() => {
const newFoo = new Foo;
const bar = newFoo.bar;
bar(123);
bar(456);
});
Run Code Online (Sandbox Code Playgroud)
更好的解决方案可能是为简单表达式编写自己的解释器,它只允许方法调用。