最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Returning a self executing Function - Stack Overflow

programmeradmin1浏览0评论

Say I want to do this:

function z(){return function(){a = 4}
}

function b(){
 var a;
 c = z();
 c();
}

I want to skip the c(); But instead I want to execute the returned function immediatly upon return in the caller scope so I can work with it.

In this example a should get the value 4.

Is there a way? cheers

Say I want to do this:

function z(){return function(){a = 4}
}

function b(){
 var a;
 c = z();
 c();
}

I want to skip the c(); But instead I want to execute the returned function immediatly upon return in the caller scope so I can work with it.

In this example a should get the value 4.

Is there a way? cheers

Share Improve this question edited Jan 7, 2013 at 17:53 Heinrich asked Jan 7, 2013 at 17:42 HeinrichHeinrich 3132 silver badges13 bronze badges 4
  • 1 b() throws an Error "'a' is not a function" – Bergi Commented Jan 7, 2013 at 17:45
  • What exactly do you mean? What scope should change? Btw, you don't need the c variable, you can just do a()(). – Bergi Commented Jan 7, 2013 at 17:47
  • a should change to 4. The function should execute in the scope of b(), which it does not when I use z()(). Already tried that before asking ;) – Heinrich Commented Jan 7, 2013 at 17:55
  • Thanks for your edits, now your question is clear. Please note that your problem actually has nothing to do with the closure, and that you do not have a self-executing function anywhere. – Bergi Commented Jan 7, 2013 at 20:36
Add a ment  | 

4 Answers 4

Reset to default 8

You should be able to execute the return function immediately with

z()();

No, that is impossible (without using tricks like eval). You cannot change the scope of the function returned by z.

Your example could be simpler without the closure, what you are asking for is just

function c() {
    a = 4;
}
function b() {
    var a;
    c(); // should change variable a
    return a;
}
b(); // is expected to return 4, but does not

You can't alter the scopes of functions, or pass the scope objects (like pointers to the variables). Yet, you can pass an object whose properties will be altered by the function:

function z(obj) {
    return function() {
        obj.a = 4;
    };
}
function b() {
    var o = {};
    z(o)(); // as mike and others said, this is the same as yours
            // - you don't need the c variable
    return o.a;
}
b(); // returns 4 now

escaparello did a similar thing, only he did use the object as the thisValue which he did pass to the anonymous function, not to z. I think mine is easier to understand, and makes a better use of the z closure.

Seems very strange, but you want to do something like this?

function z(){
    return function() {
        this.a = 4;
        return this;
    }
}

function b(){
    var obj = { a : 0 };
    var c = z().apply(obj);
    console.log(c.a);
}

b();

Try this

function z(){
    return function(){ 
        return 4;
    }
}

function b(){
    var a;
    a = z()();
}
发布评论

评论列表(0)

  1. 暂无评论