//函数要使用另一个函数的数据的两种方法: //第一种 let fn1 = function(){ let num = 10; return num; } let a = fn1();//全局变量 let fn2 =function () { console.log("值为:" + a);//因为定义了一个全局变量,所以可以用a } fn2(); //第二种 let fn4 = function(b){ console.log(‘值为:‘ + b); } let fn3 = function(){ let b = 5; fn4(b); } fn3(); //第一种 let fn5 = function () { let arr = [1, 2, 3, 4]; let sum = 0; for (let index = 0; index < arr.length; index++) { sum += arr[index]; } return sum; } let d = fn5(); let fn6 = function () { console.log("和为:" + d); } fn6(); //第二种 let fn7 = function () { let arr = [1, 2, 3, 4]; let sum = 0; for (let index = 0; index < arr.length; index++) { sum += arr[index]; } fn8(sum); } function fn8(sum) { console.log("和为:" + sum); } fn7();
原文:https://www.cnblogs.com/cj-18/p/9097559.html