admin管理员组

文章数量:1290939

In Ruby, you can capture a call to a method which is missing and define it on the fly.

What I wanna acplish in JavaScript is to have an object with no methods. I want a missing method to be translated into a call to emit():

app.isReady() -> app.emit("isReady")
soldier.kills() -> soldier.emit("kills")

I think it's better to capture the missing method error and run emit(methodName) rather than defining all methods (from a fixed list) at runtime. That way we don't have performance overhead if there are hundreds or thousands of events for an object.

What is the best way to do this?

UPDATE: This is an API design so I rather stay out of:

try {
  app.isReady()
} catch(e) {
  ...
}

I want to know how I can acplish this behind the scenes so the users can use methods as usual.

In Ruby, you can capture a call to a method which is missing and define it on the fly.

What I wanna acplish in JavaScript is to have an object with no methods. I want a missing method to be translated into a call to emit():

app.isReady() -> app.emit("isReady")
soldier.kills() -> soldier.emit("kills")

I think it's better to capture the missing method error and run emit(methodName) rather than defining all methods (from a fixed list) at runtime. That way we don't have performance overhead if there are hundreds or thousands of events for an object.

What is the best way to do this?

UPDATE: This is an API design so I rather stay out of:

try {
  app.isReady()
} catch(e) {
  ...
}

I want to know how I can acplish this behind the scenes so the users can use methods as usual.

Share Improve this question edited Aug 27, 2014 at 20:32 Paul Sweatte 24.6k7 gold badges131 silver badges268 bronze badges asked Nov 27, 2011 at 3:10 ajsieajsie 79.8k110 gold badges284 silver badges386 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

In that way we don't have a performance overhead if there are hundreds/thousands of events for an object.

I think it's a massive misconception to think the performance overhead of adding methods to an object is smaller then the performance overhead of converting method invocations into emit calls.

However you cannot implement this feature in ES5

One could however implement this using Harmony proxies.

I remend looking at simulating __noSuchMethod__.

I believe ES6 proxies are experimental and can be turned on in V8 so you could use them with node.js today.

It's not possible to do that consistently at this stage, unless you can guarantee your app will only run on Mozilla, in which case noSuchMethod is what you're after.

As far as I know, none of the other browsers implement this yet.

Use a RegExp test on the function pointer by using the following process:

  • pass the object literal as an argument
  • pass the name of the default method as a string
  • pass the name of the fallback method as a string
  • using subscript notation to dereference the function pointer
  • use a regexp to check the type against the name function
  • if it succeeds, call the default using subscript notation
  • if it fails, call the fallback using subscript notation

For example:

/* Define mapping */
var app = {"emit": emit};

/* Define interface */
function emit(){}

function \u1000missing(object, method, fallback)
  {
  /* Existence check */
  if (/function/.test(object[method]) ) 
    {
    object[method]();
    }
  /* reify */
  else
    {
    object[fallback](method)
    }    
  }

\u1000missing(app,"isReady","emit")

You might ask why you'd ever want to use subscript notation. Subscript notation allows for dynamic creation of properties and methods. So if you ever do any sort of metaprogramming, you'll most likely be using the subscript notation.

References

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

  • JSPerf: Method Missing

  • Handling a call to a missing method in different languages

  • jQuery is a Monad

  • Message forwarding in Smalltalk

  • Performance from Aligning Smalltalk and JavaScript Classes

  • Can Perl be Smalltalk?

  • How learning Smalltalk can make you a better developer

  • Reflective Facilities in Smalltalk-80

  • 12.1. QMetaObject - The Meta Object Pattern

  • Object Programming and Object-Oriented Programming

  • Things you should know about JavaScript

  • Respond to an Unknown Method Call

  • Creating Thread-Safe Components With OnMissingMethod()

  • On the design of the ECMAScript Reflection API (pdf)

  • Does Not Understand

I've create a Node.js package to deal with your situation. It's called auto-object.

Here's a glance:

const myObject = autoObject.createObject(function(name) {
    if(name.startsWith("say")) {
        return function() {
            return name.substr(3);
        }
    }

    return name;
});

myObject.foo;       ///< "foo" 
myObject.sayFoo();  ///< "Foo" 
myObject.sayBar();  ///< "Bar" 

What's more, this works with class too.

本文标签: portingCapture method missing in Javascript and do some logicStack Overflow