Doubt about the closure in Javascript

0

very good and thanks in advance for reading this.

Given a code like this:

function uno(){
    function dos(){
        alert('no debería de aparecer')
    }
    return dos;
}
var res = uno();
res()

should I not return the function two object instead of displaying the alert because one () returns the object and not the call (two ())?

This does not let me sleep. Thank you very much again.

    
asked by FER31 08.06.2018 в 02:31
source

1 answer

0

JS is very permissive lets you do almost anything ...

The first thing in JS, anything that is initialized inherits from object, so functions are objects. On the other hand you can save anything in variables including functions. (when using () you invoke them).

If you had returned two () instead of two, you would have got an error in the console even though the alert would be shown to you. But since the function one no longer returns a function but the result of two and it does not have a return, res will be undefined and when you try to run undefined it will give you an error in the console.

I hope you have clarified something but do not hesitate to ask

    
answered by 08.06.2018 в 08:00