Thursday 20 April 2017

Handle function calls on a class in Node.JS

Assuming that you have a class

class MyClass {
    world() {
        console.log("hello world");
    }
}

I can run the method similar to the following:

var hello = new MyClass();
hello.world();
# outputs: hello world

Is there a way to handle direct function calls on an object? For example:

hello();

Returns: TypeError: hello is not a function.

Can I make this call a default function? For example, similar to PHP's invoke function ...



via bashaus

No comments:

Post a Comment