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.