JavaScript closure in javascript working differently for me closure working javascript,javascript closure not working

Considering the following code snippet,

function outer(data1){
    function inner(){
        console.log(data1);
    }
    return inner;
}

in the following two function calls, first one works but not the second one.

var o = outer("sree");
o();  //works
outer("kumar");  //does not work

Please help me to understand better. Thanks.

Answer:1

Call like this:

outer("kumar")(); 

outer("kumar"); calling this will only get the reference of the inner function. By placing braces again it will call the inner function as well.

Answer:2

You're inner function has no parameters, you need to add one like this:

   function outer(){
        function inner(data1){
            console.log(data1);
        }
        return inner;
    }

You're code will always log in the console the parameter with which you created the object.

Answer:3

When you invoke the function outer("kumar") the function return a function, but that function is not invoked, that's the reason why this call do not log in console.

In the other hand, if you create a new variable invoking the function var o = outer("sree");, as I mentioned, the function outer return a function inner, then you invoke that function with the new variable created o();.

Answer:4

Similar questions have been asked before, but I'm looking for the "opposite" solution than all the others ;-) I need a JavaScript event that fires when a user decides for an option in a select field. ...

Similar questions have been asked before, but I'm looking for the "opposite" solution than all the others ;-) I need a JavaScript event that fires when a user decides for an option in a select field. ...

  1. javascript make select readonly
  2. javascript make select required
  3. javascript make select option unselectable
  4. javascript make select option selected
  5. javascript make select disabled
  6. javascript make select option disabled
  7. javascript make select box readonly
  8. javascript make select option readonly
  9. javascript make select
  10. javascript make select empty
  11. javascript make select dropdown
  12. javascript make select list
  13. javascript make select element
  14. javascript make select option hidden

This is my CSS, in which, three keyframes are bound together to make a chain animation #anim-div{ -webkit-animation-name : mpbar-anim-page1, mpbar-anim-page2, mpbar-anim-page3; animation-...

This is my CSS, in which, three keyframes are bound together to make a chain animation #anim-div{ -webkit-animation-name : mpbar-anim-page1, mpbar-anim-page2, mpbar-anim-page3; animation-...

If a new ranking is clicked how can we automatically declick the previous click? <%= f.hidden_field(:ranking, id: 'ranking') %> <%= image_tag('4.png', data: { ranking: 4 }, class: 'image-...

If a new ranking is clicked how can we automatically declick the previous click? <%= f.hidden_field(:ranking, id: 'ranking') %> <%= image_tag('4.png', data: { ranking: 4 }, class: 'image-...

  1. addclass removeclass onclick
  2. addclass removeclass click

class helloworld { constructor(public message: string){} } var hello = new helloworld('hello vishal'); Console.log(hello.message); I am getting this error at Console.log statement you can see the ...

class helloworld { constructor(public message: string){} } var hello = new helloworld('hello vishal'); Console.log(hello.message); I am getting this error at Console.log statement you can see the ...