ใช้คีย์เวิร์ด 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