我可以在 JavaScript 中定义“后备”方法吗?

问题描述 投票:0回答:2

是否可以(以高性能的方式)在 JavaScript 中定义“后备”方法?

例如

function MyObject () {
   /* what do i have to add here to have my defaultMethod? */
}

var obj = new MyObject ();
obj.doesntExistInMyObject (); // I want defaultMethod to be called
obj.doesntExistEither (); // I want defaultMethod to be called, too

即:我希望每当我编写

defaultMethod
obj.calledMethod ();
时都调用
obj.calledMethod == undefined
,但我不想在调用代码中检查
undefined

javascript
2个回答
4
投票

JavaScript 目前没有该功能。不过,通过代理,它很可能会在下一个版本中出现。所以在那之前,要做到这一点,你必须做一些相当丑陋的事情,比如:

MyObject.prototype.ex = function(fname) {
    var f    = this[fname],
        args = Array.prototype.slice.call(arguments, 1);
    if (typeof f === "function") {
        return f.apply(this, args);
    }
    return this.defaultMethod.apply(this, args);
};

...并像这样使用它:

var obj = new MyObject();
obj.ex("doesntExistInMyObject", "arg", "arg");

ex
代表“执行”,因为
call
很容易与
Function#call
混淆。)


0
投票

使用代理你可以做类似的事情

function defaultMethod(...args) {
  console.log('defaultMethod', ...args)
}

function MyObject() {
  this.hi = (name) => {
    console.log('hi', name);
  }
}

let obj = new MyObject();
obj.hi('John');

let handler = {
  get: (target, name, receiver) => {
    return (...args) => {
      let method = Reflect
        .get(target, name, receiver)

      let methodNotFound = method === undefined;

      if (methodNotFound) {
        return defaultMethod(args);
      } else {
        return method.apply(target, args);
      }
    }
  }
};
let proxyObj = new Proxy(obj, handler);

proxyObj.hi('Paul');

proxyObj.doesntExist(1, 'two', true);

© www.soinside.com 2019 - 2024. All rights reserved.