How to call a function within an anonymous function?

Asked

Viewed 1,643 times

1

Follows the code:

minhaFuncao();

Here is the code:

$(function () {
    $.connection.hub.start().done(function () {
        function minhaFuncao() {
            alert("ok");
        }
    });
});

I get error:

myFuncao is not defined at Htmlformelement.Onsuccess

  • 3

    Why is the function set there? The problem that by defining it within an anonymous function, it ceases to exist when it finishes running. That is, when you make the call, it will not exist, giving the error. There is no explanation for defining it within the event done.

  • 1

    @Matheusmiranda Why don’t you do otherwise? Set her off done and call it callback after without rewriting it.

1 answer

3


The problem of defining the function within an anonymous function is that it will only belong to the context of this function, and will cease to exist when the anonymous function has finished executing. If the issue is just calling the function within the anonymous function that treats the event done, you can define the desired function in a wider context and just call it, as the example below.

$(function () {
    function minhaFuncao() {
        alert("ok");
    }

    $.connection.hub.start().done(function () {
        minhaFuncao();
    });
});

In this way, minhaFuncao is defined in a larger context, existing anywhere within the code $(function () {...}); jQuery.

The following example was produced only to show the working of the above logic here in Sopt, since it could not reproduce the event $.connection.hub.start().done, since it depends on third party plugins.

To see in operation, just make a simpler example:

function minhaFuncao() {
  alert("ok");
}

$(function () {
  minhaFuncao();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The function itself defined as jQuery context is an anonymous function. By defining its function in the global context, it will also exist there. When the page has the DOM loaded, an alert will be displayed.

  • I think the term "anonymous function" doesn’t fit very well, because the situation would be the same even if it were a function with a defined name. It doesn’t matter if the function is expressed (and it can still have a name).

  • 1

    It’s the same operation, but the AP asked explicitly for anonymous functions. Why do you think it "doesn’t fit much"?

  • True, I didn’t care much for the title, just for the context.

  • @Andersoncarloswoss, and jQuery.when() ?

  • @Matheusmiranda, what have you?

  • The problem was calling a function that contains done correct ? With $.when is it already possible to call a function ? Here’s a jsfiddle example: http://jsfiddle.net/h4JXs/5318/ Correct me if I’m wrong.

  • 1

    No, the problem was to define a function within done, that makes no sense. There is no problem in using the done. The when is to work with Promises, I believe it would be the same thing as using the done as it is in the question. I think it would not make much sense to use also.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.