Pregunta

Is this possible with javascript? Use the same function and call it again and again.

myFunc("test")("me")("please")()

EDIT

Thanks for the answers. I'd like to keep and internal variable that appends the string to the previous one if possible.

¿Fue útil?

Solución

function MyFunc(arg){
    console.log(arg);
    return MyFunc;
}

MyFunc("a")("b")("c");

JsFiddle example

Y-combinator example:

function Y(f) {
    var g = f(function() {
        return g.apply(this, arguments);
    });
    return g;
}

var MyFunc = Y(function(f) {
    var a = "";
    return function(n) {
        a = a + n;
        console.log(a);
        return f;
    };
});

//alert(a); // throws error as a is outside of scope here
MyFunc("a")("b")("c"); # logs a; ab; abc

Otros consejos

Yes, you can, if you will return arguments.callee;, example:

(function(param){console.log(param); return arguments.callee;})("test")("me")("please")

will log into console:

test
me
please 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top