JavaScript - 从匿名函数返回(varScope)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7229767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:24:27  来源:igfitidea点击:

JavaScript - Return from anonymous function (varScope)

javascriptscopeanonymous-function

提问by headacheCoder

<script>
    var sample = function() {
        (function() {
            return "something"
        })();
        // how can I return it here again?
    }
</script>

Is there a way to return the returned value from the anonymous function in the parent function again or do I need to use a defined function to get the returned value? Thanks! :)

有没有办法再次从父函数中的匿名函数返回返回值,或者我是否需要使用定义的函数来获取返回值?谢谢!:)

采纳答案by Quentin

Just put the return statement at the point where you call the function.

只需将 return 语句放在调用函数的位置即可。

<script>
    var sample = function() {
        return (function() {  // The function returns when you call it
            return "something"
        })();
    }
</script>