JavaScript - Return from anonymous function (varScope)

拈花ヽ惹草 提交于 2019-12-13 12:00:21

问题


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


回答1:


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

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


来源:https://stackoverflow.com/questions/7229767/javascript-return-from-anonymous-function-varscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!