What happens when you call call when JS implements the intercept agent for get and apply for the function?

< H1 > problem description < / H1 >

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 >
  1. found that both the get and apply intercepting methods were called when the call method was called on the proxy object. But notice that the get method returns target [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 to call is also not called on the proxy object , so why trigger the apply interception?
  2. assumes that call and apply behaviors are intercepted by apply , so it seems that the value returned by the get method does not matter, because its return value does not affect the use of apply . But if the get method returns a non-functional object , such as 1 , the runtime triggers an error: TypeError: animalProxy.call is not a function .
< H1 > extension problem < / H1 >

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?

Nov.24,2021

< H2 > question 1: < / H2 >

what you return in get is just the call function, and its caller is still proxy , so it also triggers apply

.

you can run
add with the following example and output get three times, that is, within the add function this pointing to whether it should or proxy
minus with the arrow function will only be executed once, because the arrow function binds the outer this by default and will not change because of the caller, so it will not be changed.

  

					
Menu