Are there equivalents to Ruby's method_missing in other languages?

前端 未结 15 1631
渐次进展
渐次进展 2020-12-08 08:00

In Ruby, objects have a handy method called method_missing which allows one to handle method calls for methods that have not even been (explicitly) defined:

15条回答
  •  一整个雨季
    2020-12-08 08:35

    JavaScript has noSuchMethod, but unfortunately this is only supported by Firefox/Spidermonkey.

    Here is an example:

    wittyProjectName.__noSuchMethod__ = function __noSuchMethod__ (id, args) {
       if (id == 'errorize') {
        wittyProjectName.log("wittyProjectName.errorize has been deprecated.\n" +
                             "Use wittyProjectName.log(message, " +
                             "wittyProjectName.LOGTYPE_ERROR) instead.",
                             this.LOGTYPE_LOG);
        // just act as a wrapper for the newer log method
        args.push(this.LOGTYPE_ERROR);
        this.log.apply(this, args);
      }
    }
    

提交回复
热议问题