there is a function called Animal that prints out the relevant statements. Then use the proxy to implement the get and apply methods:
function Animal() {
console.log("This is a " + this.species);
}
var animalProxy = new Proxy(Animal, {
get: function(target, prop, recevier) {
console.log("Proxy get was invoked!");
return target[prop];
},
apply: function(target, context, args) {
console.log("Proxy apply was invoked!");
return Reflect.apply(...arguments);
}
});
animalProxy implements the proxy for the function Animal . Now, there is the following statement
console.log( animalProxy.call({
species: "Cat"
} ));
it also runs as expected, outputting
Proxy get was invoked!
Proxy apply was invoked!
This is a Cat
undefined
< H1 > question < / H1 >
- found that both the
getandapplyintercepting methods were called when thecallmethod was called on the proxy object. But notice that thegetmethod returnstarget [prop]. Here, I think it should return the call method of the original object (that is,Animal), which has nothing to do with the proxy object. Now that a method that has nothing to do with the proxy object has been returned, then the function call tocallis also not called on the proxy object , so why trigger theapplyinterception? - assumes that
callandapplybehaviors are intercepted byapply, so it seems that the value returned by thegetmethod does not matter, because its return value does not affect the use ofapply. But if thegetmethod returns a non-functional object , such as1, the runtime triggers an error:TypeError: animalProxy.call is not a function.
suppose I output a arguments parameter list in each intercept method, that is,
var animalProxy = new Proxy(Animal, {
get: function(target, prop, recevier) {
console.log("Proxy get was invoked!",arguments); //
return target[prop];
},
apply: function(target, context, args) {
console.log("Proxy apply was invoked!",arguments); //
return Reflect.apply(...arguments);
}
});
then using the console.log (animalProxy.call ({species: "Cat"})); statement) runs normally in the Chrome console environment, but the stack will burst in Nodejs (v10.6.0), and it will overflow the stack in the console statement in get :
< console.log("Proxy get was invoked!",arguments);
< ^
< RangeError: Maximum call stack size exceeded
however, if you comment out arguments in get but retains apply in arguments , then it works fine in the Nodejs (v10.6.0) environment.
Why is this? Is it related to the Nodejs version problem?
