简体   繁体   English

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

[英]JavaScript - Return from anonymous function (varScope)

<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! 谢谢! :) :)

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>

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

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