JavaScript 函数调用并返回结果


对于外部函数调用,请使用 return 关键字。代码如下所示 −

示例

var substractMethod = function () {
   var firstValue =1000, thirdValue= 200;
   var divideMethod = function (){
      var secondValue =500;
      console.log("The result of
      divideMethod()="+(firstValue/secondValue));
      return (firstValue-secondValue);
   }
   return divideMethod;
}
var result = subtractMethod();
console.log("The result of substractMethod()="+result());

要运行上述程序,你需要使用以下命令 −

node fileName.js.

此处,我的文件名是 demo198.js。

输出

此命令将生成以下输出 −

PS C:\Users\Amit\javascript-code> node demo198.js
The result of divideMethod()=2
The result of subtractMethod()=500

更新于: 14-Sep-2020

114 次浏览

助力你的 职业生涯

完成课程,获得认证

开始学习
廣告
© . All rights reserved.