Built-in Objects
Built-in Objects là gì?
Phần tiêu đề “Built-in Objects là gì?”Built-in objects (còn gọi là global objects) là những đối tượng được tích hợp sẵn trong ngôn ngữ JavaScript, có thể sử dụng ở mọi nơi trong chương trình mà không cần khai báo hoặc import.
Các Built-in Objects phổ biến
Phần tiêu đề “Các Built-in Objects phổ biến”| Đối tượng | Chức năng chính |
|---|---|
| Number | Làm việc với số: toFixed(), toPrecision(), toString(), isNaN(), isFinite() |
| Math | Hằng số và hàm toán học: Math.PI, Math.sqrt(), Math.pow(), Math.abs(), Math.floor(), Math.ceil(), Math.round(), Math.random() |
| Date | Thời gian và ngày tháng: getFullYear(), getMonth(), getDate(), getHours(), toDateString(), toLocaleString() |
| String | Xử lý chuỗi: length, toUpperCase(), toLowerCase(), substring(), slice(), replace(), includes(), split(), trim(), charAt() |
| Boolean | Giá trị logic: valueOf(), toString() |
| Function | Các hàm: call(), apply(), bind(), length, name, toString() |
| Error | Xử lý lỗi: message, name, stack |
| Array | Mảng: push(), pop(), shift(), unshift(), map(), filter(), reduce(), forEach(), find(), includes(), sort() |
| Object | Đối tượng: keys(), values(), entries(), assign(), hasOwnProperty(), toString() |
| Map | Bản đồ: set(), get(), has(), delete(), clear(), size |
| Set | Tập hợp: add(), delete(), has(), clear(), size |
Ví dụ
Phần tiêu đề “Ví dụ”Math.PI; // 3.141592653589793Math.sqrt(16); // 4Math.floor(4.7); // 4Math.ceil(4.2); // 5Math.round(4.5); // 5Math.random(); // Số ngẫu nhiên 0 → 1Math.abs(-5); // 5Math.pow(2, 3); // 8let arr = [1, 2, 3, 4, 5];
arr.map(x => x * 2); // [2, 4, 6, 8, 10]arr.filter(x => x > 2); // [3, 4, 5]arr.reduce((a, b) => a + b); // 15arr.find(x => x === 3); // 3arr.includes(4); // true// Set — chỉ chứa giá trị duy nhấtlet set = new Set([1, 2, 2, 3, 3, 3]);console.log(set); // Set { 1, 2, 3 }set.has(2); // trueset.size; // 3
// Map — key có thể là bất kỳ kiểu gìlet map = new Map();map.set("name", "Alice");map.set(42, "answer");map.get("name"); // "Alice"let now = new Date();now.getFullYear(); // 2026now.getMonth(); // 0-11 (tháng 1 = 0)now.getDate(); // 1-31now.toDateString(); // "Mon Feb 09 2026"now.toLocaleString(); // string theo locale