Từ khóa function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 trong JavaScript là từ khóa để xây dựng câu lệnh kết thúc quá trình thực thi mã JavaScript. Ví dụ: giả sử bạn có đoạn mã sau chạy hai nhật ký bảng điều khiểnconsole.log("Hello");
return;
console.log("How are you?");
Bạn sẽ thấy rằng chỉ có “Xin chào” được in ra bảng điều khiển, trong khi “Bạn có khỏe không?” không được in vì từ khóa function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2. Nói tóm lại, câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 sẽ luôn dừng mọi thực thi trong ngữ cảnh JavaScript hiện tại và trả lại quyền kiểm soát cho ngữ cảnh cao hơn. Khi bạn sử dụng function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 bên trong function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
6, function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
6 sẽ ngừng chạy và trả lại quyền điều khiển cho người gọiHàm sau sẽ không in “Xin chào từ hàm” ra bảng điều khiển vì câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 được thực thi trước lệnh gọi ghi chuỗi. Sau đó, JavaScript sẽ trả lại quyền kiểm soát cho người gọi và tiếp tục thực thi mã từ đófunction fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
Bạn có thể thêm một biểu thức vào câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 để thay vì trả về function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
3, giá trị mà bạn chỉ định sẽ được trả vềHãy xem ví dụ mã sau, trong đó hàm function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
4 chỉ có giá trị là function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 mà không có bất kỳ giá trị nào, trong khi hàm function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
6 trả về một số function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
7function fnOne() {
return;
}
function fnTwo() {
return 7;
}
let one = fnOne();
let two = fnTwo();
console.log(one); // undefined
console.log(two); // 7
Trong đoạn mã trên, các giá trị được trả về từ câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 của mỗi hàm được gán cho các biến function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
9 và function fnOne() {
return;
}
function fnTwo() {
return 7;
}
let one = fnOne();
let two = fnTwo();
console.log(one); // undefined
console.log(two); // 7
0. Khi câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 sẽ trả lại function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
3 theo mặc địnhBạn cũng có thể gán một biến cho câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 như saufunction fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
0Và đó là tất cả về từ khóa function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 trong JavaScript. Bạn có thể kết hợp câu lệnh function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
2 và khối điều kiện function fnOne() {
return;
}
function fnTwo() {
return 7;
}
let one = fnOne();
let two = fnTwo();
console.log(one); // undefined
console.log(two); // 7
6 để kiểm soát giá trị nào được trả về từ function fnExample() {
return;
console.log("Hi from function");
}
fnExample(); // nothing will be printed
console.log("JavaScript");
console.log("Continue to execute after return");
6 của bạn
Bạn có thể quay lại bằng JavaScript không?
Chúng ta có thể trả về các giá trị nguyên thủy (chẳng hạn như Boolean, số, chuỗi, v.v. ) và các loại Đối tượng (chẳng hạn như hàm, đối tượng, mảng, v.v. ) bằng cách sử dụng câu lệnh return . Chúng ta cũng có thể trả về nhiều giá trị bằng câu lệnh return.
Hàm JavaScript có kiểu trả về không?
như ví dụ thứ hai của bạn cho thấy, không có thứ gọi là "kiểu trả về của hàm" trong Javascript .
Làm cách nào để trả lại đối tượng trong JavaScript?
Để trả về một đối tượng từ hàm JavaScript, hãy sử dụng câu lệnh return với từ khóa này .
Tôi có thể sử dụng cái gì thay vì trả về trong JavaScript?
Nói một cách đơn giản phần tiếp theo là một hàm được sử dụng thay cho câu lệnh trả về. Chính thức hơn. Tiếp tục là một chức năng được gọi bởi một chức năng khác. Điều cuối cùng mà chức năng khác thực hiện là gọi phần tiếp theo. |