简体   繁体   English

如何从匿名函数获取值?

[英]How to get a value from anonymous function?

Here is the code and fiddle : 这是代码和小提琴

var test = function(){
    return{
        value: "hi",
        func1: function(){
            func2();
        }()
    };
}();

function func2(){
    alert(test.value);
}

Can anyone tell me what have I done wrong? 谁能告诉我我做错了什么? I can't get test.value. 我无法获得test.value。

Your func2() is called from the IIFE for the func1 property in the IIFE that constructs the object to be assigned to test . IIFE中IIFE中的func1属性调用func2() ,该属性构造要分配给test的对象。 Before it assigns to test . 在分配test之前。 And it tries to access the test.value property, on a variable that is yet undefined , and throws a ReferenceError therefore. 并且它尝试访问尚未undefined的变量的test.value属性,并因此引发ReferenceError (See also Self-references in object literal declarations ) (另请参见对象文字声明中的自我引用

I think you are looking for 我想你在找

var test = function(){
    return{
        value: "hi",
        func1: function(){
            func2();
        } // <-- no invocation here, make `func1` a method not `undefined`
    };
}();

function func2(){
    alert(test.value);
}

test.func1(); // "hi"

( updated fiddle ) 更新小提琴

If you run this code you'll be getting an undefined . 如果运行此代码,将会得到undefined Why? 为什么? Well because you haven't given enough time for your test to actually initiate itself ( and the fact that what you're doing this in a synchronous way). 好吧,因为您没有给test足够的时间来实际启动自身 (以及您正在以同步方式进行操作的事实)。

Try this asynchronous solution. 试试这个异步解决方案。

var test = function(){
    return{
        value: "hi",
        func1: function(){
            setTimeout(function(){ func2(); }, 50);
          //you can also just do
          //setTimeout(func2, 50);
        }()
    };
}();

function func2(){
    alert(test.value);
}

Demo 演示

Also notice that you don't return anything to your func1 since it auto invoques itself. 还要注意,由于func1本身会自动发出发票,因此您不会将任何内容返回给func1。 Later on if you try to do test.func1() it won't work. 稍后,如果您尝试执行test.func1() ,它将无法正常工作。

The problem is that you assign an object literal to test . 问题是您将对象文字分配给test However, to build that object, you call func2 . 但是,要构建该对象,请调用func2 That function attempts to access test , but at that point it still is undefined. 该函数尝试访问test ,但是到那时它仍未定义。

Therefore, you should call func2 after assigning the object to test : 因此,在将对象分配给test之后,应该调用func2

var test = function(){
    return {
        value: "hi",
    };
}();
test.func1 = function(){
    func2();
}();
function func2(){
    alert(test.value);
}

However, note that's a bit weird, because the anonymous function doesn't return any value. 但是,请注意,这有点奇怪,因为匿名函数不会返回任何值。 func2(); test.func1 = undefined func2(); test.func1 = undefined would be more natural. func2(); test.func1 = undefined会更自然。

As Bergi mentioned you want to use Imidiatelly Invoked Function Expression (IIFE) but your syntax is wrong. 如Bergi所述,您想使用Imidiatelly调用函数表达式(IIFE),但是您的语法错误。

see fixed fiddle 看到固定的小提琴

var test = (function(){
    return{
        value: "hi",
        func1: function(){
            func2();
        }
    };
})();

function func2(){
    alert(test.value);
}

With ( at the start of your function and at the end of it before you actually immediately call it 在函数的开头和结尾处使用(在实际立即调用它之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM