简体   繁体   中英

Javascript: how to print return value of function?

I'm beginner to Javascript and reading Closures and Variables part of book "JS for web developers". It gives two examples:

function Func1() {
    var result = new Array();
    for (var i=0; i < 10; i++){
        result[i] = function() {
           console.log('i:'+i);
           return i; 
        };
    }

    return result;
}

console.log(Func1());

function Func2() {
    var result = new Array();

    for(var i=0; i < 10; i++){
        result[i] = function(num) {
            return function() {
                console.log('num:'+num);
                return num;
            };
        }(i);
    }

    return result;
}

console.log(Func2());

In the book's description, it said in func1, every function could return 10, while in func2, each function would return different number. But when I run the code, it actually returns:

[ [Function],
  [Function],
  [Function],
  [Function],
  [Function],
  [Function],
  [Function],
  [Function],
  [Function],
  [Function] ]

for both functions.

So how to print out actual values for each function? And why "console.log('i:'+i);" is not printed in first function?

Try this wrap your result[i] data into a invoked function to get the data you need...

check this:

function Func1() {
    var result = new Array();
    for (var i=0; i < 10; i++){
        result[i] = (function() {
           console.log('i:'+i);
           return i; 
        })();
    }

    return result;
}

console.log(Func1());

You are returning a function in your for loop, instead you should just return the value inside of your closure.

function Func2() {
    var result = new Array();

    for(var i=0; i < 10; i++){
        result[i] = function(num) {
            console.log('num:'+num);
            return num;
        }(i);
    }

    return result;
}

console.log(Func2());

Given the current output of the execution of the function, an Array of functions, you can iterate the result of FuncN then execute the function at each element of the array

for (let n = 0, f = Func1(); n < f.length; n++) {
  console.log(f[n]())
}

Try them like this:

function Func1() {
    var result = new Array();
    for (var i=0; i < 10; i++){
        result[i] = function() {
           console.log('i:'+i);
           return i; 
        }();
    }
return result;

}

console.log(Func1());

function Func2() {
    var result = new Array();

    for(var i=0; i < 10; i++){
        result[i] = function(num) {
            return function() {
                console.log('num:'+num);
                return num;
            }();
        }(i);
    }

    return result;
}

console.log(Func2());

the problem is due the fact that you have set the function as it's return value rather than calling it and then returning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM