JavaScript Equivalent Of PHP __call

前端 未结 5 1361
一生所求
一生所求 2020-12-05 09:55

In PHP you can detect when a method is called even when it doesn\'t exist using the \"magic\" __call function.

public function __call($methodNam         


        
5条回答
  •  离开以前
    2020-12-05 10:36

    It is possible using the ES6 Proxy API:

    var myObj = {};
    var myProxy = new Proxy(myObj, {
      get: function get(target, name) {
        return function wrapper() {
          var args = Array.prototype.slice.call(arguments);
          console.log(args[0]);
          return "returns: " + args[0];
        }
      }
    });
    console.log(myProxy.foo('bar'));

    Browser compatibility is available on MDN. As of August 2017 all browsers (including Microsoft Edge) except Internet Explorer support it.

    See this answer for a more complete look at Proxy.

提交回复
热议问题