Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return value from anonymous function in js

I would like to return value from anonymous function. How can I assign returned value to $id variable in below code ?

$(document).on("click", '.delete-component', function(e) {
     return 4;
 });

 //$id in this scope should be equal 4
like image 988
Thomas Shelby Avatar asked Dec 02 '14 13:12

Thomas Shelby


People also ask

Can anonymous functions return a value?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.

Can anonymous function return JavaScript?

They are always invoked (called) using the variable name. Also, we create anonymous functions in JavaScript, where we want to use functions as values. In other words, we can store the value returned by an anonymous function in a variable.

How do you use anonymous function in JavaScript?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

What is return () in JavaScript?

The return statement ends function execution and specifies a value to be returned to the function caller.


2 Answers

You need to specify what to do when asyn function or operation is reached, so usually a return when using callbacks is another callback...

function aux(param1, callback){
    // do whatever...
    $(document).on("click", param1, function(e) {
        // return 4;
        callback(4);
    });
}

and you would use it in you code as following:

// your context..
// operations 1.. 2..
aux('.delete-component', function(theReturnedValue){
    console.log(theReturnedValue); // 4
});

This is how callbacks 'return' values to an outer scope.

like image 153
rahpuser Avatar answered Sep 27 '22 22:09

rahpuser


One thing you have to be aware that you work here with asynchronous actions. Let's number lines in order of exaction (n means some high number far far later)

1]    $(document).on("click", '.delete-component', function(e) {
n+1]    return 4;
      });
2]    console.log('here');

What you did was attached listener to click. Click won't happen at the moment - it will happen when someone clicks. Therefore you will have access to it after click happens. You can do two things:

  1. Declare var in scope above.
  2. Forward value to callback

1) Example 1

var myValue = 0;
$(document).on("click", '.delete-component', function(e) {
     myValue = 4;
});

function abc() {
  console.log('myValue is equal to ' + myValue);
}

// if that line happen before clicking, value will be still 0
execture somewhen abc();

2) Example 2

$(document).on("click", '.delete-component', function(e) {
     doSomethingWithValue(4);
});

function doSomethingWithValue() {
  console.log('myValue is equal to ' + myValue);
}

You can also investigate $watch, especially Angular does here a lot work for you.

like image 43
Lukasz Marek Sielski Avatar answered Sep 27 '22 22:09

Lukasz Marek Sielski