JavaScript - Return from anonymous function (varSc

2019-04-20 06:02发布

<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条回答
太酷不给撩
2楼-- · 2019-04-20 06:39

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>
查看更多
登录 后发表回答