Sử dụng từ khóa trả về cho lệnh gọi hàm người ngoài. Sau đây là mã -
Ví dụ
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()); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo198.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\javascript-code> node demo198.js The result of divideMethod()=2 The result of subtractMethod()=500