user2507818
user2507818

Reputation: 3037

trying to understand the execution sequence in some js codes

<script type="text/javascript"> 
var DYN_WEB = DYN_WEB || {};
DYN_WEB.Util = (function( Ut ) {
    Ut.getResult = function ( cl, tag, el ) {
    console.log(arguments)
    }
return Ut;
})( DYN_WEB.Util || {} );
var links = DYN_WEB.Util.getResult('show-hide');
</script>  

In chrome->console, it shows: ["show-hide"]

Question:

when script goes to this line:DYN_WEB.Util, did not reach var links yet, why console.log(arguments) can still outputs ["show-hide"] , not undefined?

Upvotes: 0

Views: 47

Answers (2)

Code Lღver
Code Lღver

Reputation: 15603

DYN_WEB.Util is a function and will not execute itself. So you are wrong at the point that it not reach at the var links.

First it will reach at the var links from where it will call to DYN_WEB.Util function and it will pass the arguments that you are using.

So the flow of calling will be first it call to var links from where it call to DYN_WEB.Util function that it initialize above and then execute the console.log.

And it output the show-hide.

Upvotes: 0

Alex Filipovici
Alex Filipovici

Reputation: 32561

You could add some extra lines if you want to understand the execution flow. The getResult function is called on the links line. Try this:

var DYN_WEB = DYN_WEB || {};
DYN_WEB.Util = (function (Ut) {
    Ut.getResult = function (cl, tag, el) {
        console.log(arguments)
    }
    return Ut;
})(DYN_WEB.Util || {});
console.log("before links");
var links = DYN_WEB.Util.getResult('show-hide');
console.log("after links");

The console output will be:

before links
["show-hide"]
after links 

Upvotes: 1

Related Questions