<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! :)
No, you cannot return a value from an asynchronous callback. Event callback will be called on click, but you can't assign its result.
The () makes the anonymous function an expression that returns a function object. An anonymous function is not accessible after its initial creation. Therefore, you often need to assign it to a variable. In this example, the anonymous function has no name between the function keyword and parentheses () .
They're called anonymous functions because they aren't given a name in the same way as normal functions. Because functions are first-class objects, we can pass a function as an argument in another function and later execute that passed-in function or even return it to be executed later.
Conclusion. An anonymous function is a function with no name which can be used once they're created. The anonymous function can be used in passing as a parameter to another function or in the immediate execution of a function.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With