Ngôn ngữ tương tác cho Web
Master JavaScript functions from basics to advanced concepts including closures, higher-order functions, async/await, and functional programming patterns.
Essential concepts every JavaScript developer should understand
Hàm được khai báo với từ khóa function và có thể được gọi trước khi định nghĩa do hoisting.
function chaoHoi(ten) { return `Xin chào, ${ten}!`; }
Hàm được gán cho một biến và không có hoisting.
const chaoHoi = function(ten) { return `Xin chào, ${ten}!`; };
Cú pháp ngắn gọn cho hàm, không có this riêng và không có hoisting.
const chaoHoi = (ten) => `Xin chào, ${ten}!`;
Hàm có thể truy cập biến từ phạm vi bên ngoài ngay cả sau khi phạm vi đó đã kết thúc.
function ngoai(x) { return function(y) { return x + y; }; }
Hàm nhận hàm khác làm tham số hoặc trả về một hàm.
function map(mang, hamXuLy) { return mang.map(hamXuLy); }
Hàm được thực thi ngay lập tức sau khi được định nghĩa.
(function() { console.log('IIFE đã thực thi!'); })();
Key differences between function declarations, expressions, and arrow functions
| Aspect | Function Declaration | Function Expression | Arrow Function |
|---|---|---|---|
| Hoisting | Có hoisting đầy đủ | Không có hoisting | Không có hoisting |
| this binding | Dynamic this | Dynamic this | Lexical this |
| arguments object | Có sẵn | Có sẵn | Không có |
| Constructor | Có thể dùng new | Có thể dùng new | Không thể dùng new |
| Cú pháp | function name() {} | const name = function() {} | const name = () => {} |
Follow these guidelines for clean, maintainable function code
Sử dụng tên động từ mô tả chức năng của hàm
Good Practice Example
// Tốtfunction calculateTotalPrice(items) { ... }function validateEmail(email) { ... }// Không tốtfunction calc(items) { ... }function check(email) { ... }
Mỗi hàm chỉ nên có một trách nhiệm duy nhất (Single Responsibility)
Good Practice Example
// Tốtfunction calculateTax(price) { ... }function formatCurrency(amount) { ... }// Không tốtfunction calculateAndFormatPrice(price) {// Làm nhiều việc cùng lúc}
Tránh reassignment không mong muốn
Good Practice Example
// Tốtconst calculateTotal = (items) => { ... };// Không tốtlet calculateTotal = (items) => { ... };
Sử dụng early return để code dễ đọc hơn
Good Practice Example
// Tốtfunction processUser(user) {if (!user) return null;if (!user.isActive) return null;return processActiveUser(user);}// Không tốtfunction processUser(user) {if (user) {if (user.isActive) {return processActiveUser(user);}}return null;}
Avoid these frequent pitfalls when working with functions
⚠️ Problem
// LỗisayHello(); // ReferenceErrorconst sayHello = () => console.log("Hello");// Đúngconst sayHello = () => console.log("Hello");sayHello(); // "Hello"
✅ Solution
Khai báo function expression trước khi sử dụng
⚠️ Problem
// Lỗiconst obj = {name: "Test",greet: () => console.log(this.name) // undefined};// Đúngconst obj = {name: "Test",greet() { console.log(this.name) } // "Test"};
✅ Solution
Sử dụng regular function cho object methods
⚠️ Problem
// Lỗiconst calculate = (x, y) => {const result = x * y;// Quên return};// Đúngconst calculate = (x, y) => {const result = x * y;return result;};
✅ Solution
Luôn có return statement khi cần giá trị trả về