The capture method is missing in Javascript and does some logic?

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

What I want to do in JavaScript is to have an object without methods. I want the missing method to be translated into an emit () call:

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

I think it is better to fix the missing method error and run emit (methodName), rather than defining all methods (from a fixed list) at runtime. Thus, we do not 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 prefer not to allow:

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

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

+4
javascript reification sample porting method-missing
Nov 27 '11 at 3:10
source share
4 answers

Thus, we do not have performance overhead if there are hundreds / thousands of events for an object.

I think it’s a massive misconception to think that the performance overhead when adding methods to an object is less than the performance overhead when converting method calls to outgoing calls.

However, you cannot implement this feature in ES5

However, you can implement this using Harmony proxies .

I recommend watching the __noSuchMethod__ simulation .

I believe that ES6 proxies are experimental and can be included in V8, so you can use them with node.js today.

+6
Nov 27 2018-11-11T00:
source share
— -

This cannot be done at this stage if you cannot guarantee that your application will work only in Mozilla, in which case noSuchMethod is what you need.

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

+2
Nov 27 '11 at 3:52
source share

Use RegExp test in the function pointer using the following process:

  • pass object literal as argument
  • pass the default method name as a string
  • pass the name of the backup method as a string
  • using index notation to dereference a function pointer
  • use regex to check type called function
  • if that succeeds, call the default using substring notation
  • If this fails, call the backup using index 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 may ask why you would like to use substring notation. The substring designation allows you to dynamically create properties and methods. Therefore, if you ever perform any metaprogramming, you will most likely use musical notation.

References

0
Apr 04 '14 at 19:31
source share

I created Node.js package to solve your situation. It was called auto-object .

Here is a look:

 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 else, this also works with the class.

0
Jun 17 '17 at 12:57
source share



All Articles