- Trang chủ
- Tutorials
- JavaScript
- Javascript cơ bản
Built - in Object là gì?
Built - in Object là các đối tượng được xây nhà cung cấp xây dựng sẵn (hay còn gọi là được tích hợp sẵn). Chúng ta chỉ cần lấy ra và sử dụng.
Ngoài console, còn có các đối tượng (object) khác được tích hợp sẵn vào JavaScript. Sau này bạn sẽ biết cách xây dựng đối tượng của riêng bạn , nhưng bây giờ những đối tượng có sẵn này có đầy đủ các chức năng hữu ích.
Built-in Object - Đối tượng được tích hợp sẵn trong Javascript
Ví dụ: Nếu bạn muốn thực hiện các phép toán phức tạp hơn, JavaScript có đối tượng (object) Math được tích hợp sẵn.
Điều tuyệt vời về các đối tượng là chúng có các phương thức (method) có sẵn theo nó! Hãy gọi phương thức .random() từ đối tượng Math được tích hợp sẵn:
Thử nghiệm với đối tượng Math được JS tích hợp sẵn
Trong ví dụ trên, chúng ta đã gọi phương thức .random() bằng cách nối thêm tên đối tượng với tên của phương thức (medthod) bằng toán tử dấu chấm (dot operator), và mở và đóng dấu ngoặc đơn.
Phương thức này trả về một số ngẫu nhiên giữa 0 và 1.
Để tạo một số ngẫu nhiên từ 0 đến 50, chúng tôi có thể nhân kết quả này với 50, như sau:
Ví dụ, ở trên có thể sẽ kết quả nhận được sẽ là số thập phân. Để đảm bảo kết quả là một số nguyên, chúng ta có thể tận dụng một phương thức hữu ích khác của Math gọi là Math.floor().
Trường hợp này máy tính sẽ làm như sau:
- Math.random() tạo ra một số ngẫu nhiên giữa 0 và 1.
- Sau đó nhân số đó với 50, vì vậy bây giờ chúng ta có một số từ 0 đến 50
- Sau đó, Math.floor() làm tròn số xuống số nguyên gần nhất.
Để xem tất cả các thuộc tính và phương thức của đối tượng Math, hãy xem tài liệu chuẩn ở đây
Bài tập thực hành sử dụng đối tượng được tích hợp sẵn.
Để hiểu hơn về Object trong Javascript, hãy thử làm các bài tập sau. Sử dụng tài liệu chuẩn để tìm kiếm phương thức có sẵn của đối tượng Math để:
- In ra màn hình console 1 số bất kỳ trong khoảng từ 1- 100
- In ra màn hình số nguyên đã làm tròn xuống của cả số sau:
- (26 + 35)
- (2018 - 1969)
- (65 / 100)
- (0.6969 * 100)
- In ra màn hình số nguyên bất kỳ trong khoảng từ 1- 100
- Sử dụng JavaScript Math Object tại đây để làm in ra màn hình console số nguyên nhỏ nhất lớn hơn hoặc bằng 69.96
- Sử dụng tài liệu chuẩn về JavaScript tại đây để kiểm tra xem 2018 có phải là số nguyên hay không, in kết quả ra màn hìn console.
Đăng ký tư vấn
Nhân viên gọi điện tư vấn miễn phí sau khi đăng ký
Được cập nhật các ưu đãi sớm nhất
Hotline: 0383180086
Hãy đăng ký để nhận những thông tin mới nhất về học bổng mới nhất tại NIIT - ICT Hà Nội
This chapter documents all of JavaScript's standard, built-in objects, including their methods and properties. The term "global objects" (or standard built-in objects) here is not to be confused with the global object. Here, "global objects" refer to objects in the global scope. The global object itself can be accessed using the
this operator in the global scope. In fact, the global scope consists of the properties of the global object, including inherited properties, if any. Other objects in the global scope are either created by the user script or
provided by the host application. The host objects available in browser contexts are documented in the API reference. For more information about the distinction between the DOM and core JavaScript, see
JavaScript technologies overview. These global properties return a simple value. They have no properties or methods.Standard objects by category
Value properties
Function properties
These global functions—functions which are called globally, rather than on an object—directly return their results to the caller.
- eval()
- isFinite()
- isNaN()
- parseFloat()
- parseInt()
- encodeURI()
- encodeURIComponent()
- decodeURI()
- decodeURIComponent()
- Deprecated
- escape() Deprecated
- unescape() Deprecated
Fundamental objects
These are the fundamental, basic objects upon which all other objects are based. This includes general objects, booleans, functions, and symbols.
- Object
- Function
- Boolean
- Symbol
Error objects
Error objects are a special type of fundamental object. They include the basic Error type, as well as several specialized error types.
- Error
- AggregateError
- EvalError
- InternalError Non-standard
- RangeError
- ReferenceError
- SyntaxError
- TypeError
- URIError
Numbers and dates
These are the base objects representing numbers, dates, and mathematical calculations.
- Number
- BigInt
- Math
- Date
Text processing
These objects represent strings and support manipulating them.
- String
- RegExp
Indexed collections
These objects represent collections of data which are ordered by an index value. This includes (typed) arrays and array-like constructs.
- Array
- Int8Array
- Uint8Array
- Uint8ClampedArray
- Int16Array
- Uint16Array
- Int32Array
- Uint32Array
- Float32Array
- Float64Array
- BigInt64Array
- BigUint64Array
Keyed collections
These objects represent collections which use keys. The iterable collections (Map and Set) contain elements which are easily iterated in the order of insertion.
- Map
- Set
- WeakMap
- WeakSet
Structured data
These objects represent and interact with structured data buffers and data coded using JavaScript Object Notation (JSON).
- ArrayBuffer
- SharedArrayBuffer
- Atomics
- DataView
- JSON
Control abstraction objects
Control abstractions can help to structure code, especially async code (without using deeply nested callbacks, for example).
- Promise
- Generator
- GeneratorFunction
- AsyncFunction
- AsyncGenerator
- AsyncGeneratorFunction