Built-in objects
- foreword
- 1. MDN document
-
- MDN documentation link:
- How to learn about objects in MDN
- Two, Math object
-
- 2.1 Overview of Math
- 2.2 Random number method Random()
- 3. Date object
-
- 3.1 Overview of Date
- 3.2 Use of the Date() method
- 3.3 Date formatting
- 3.4 Get the total milliseconds of the date
- 4. Array object
-
- 4.1 Creation of array objects
- 4.2 Check if it is an array
- 4.3 Methods of adding and deleting array elements
- 4.4 Array sorting
- 4.5 Array Indexing Methods
- 4.6 Convert array to string
- 4.7 Other methods
- 5. String object
-
- 5.1 String properties
- 5.2 Return location based on content
- 5.3 Return characters by position
- 5.4 String manipulation methods
Foreword
1. There are three types of objects in JavaScript: custom objects, built-in objects, and browser objects. The first two objects are the basic content of JS and belong to ECMAScript; the third browser object belongs to our JS unique
2. Built-in objects refer to some objects that come with the JS language. These objects are used by developers and provide some commonly used or the most basic and necessary functions (properties and methods)
3. The biggest advantage of built-in objects is to help us develop quickly
4. JavaScript provides multiple built-in objects: Math, Date, Array, String, etc.
1. MDN documentation
MDN documentation link:
MDN documentation: https://developer.mozilla.org/zh-CN/
How to learn about objects in MDN
- Check out what this method does
- Check the meaning and type of the parameters inside
- See the meaning and type of the return value
- Test with demo
2. Math object
2.1 Math Overview
The Math object is not a constructor, it has properties and methods for mathematical constants and functions. Operations related to mathematics (absolute value, rounding, maximum value, etc.) can use the members of Math
The code is as follows (example):
Math.PI // Pi Math.floor() // round down Math.ceil() // round up Math.round() // The rounded version is rounded to the nearest integer. Note that -3.5 results in -3 Math.abs() // absolute value Math.max() // Find the maximum value Math.min() // Find the minimum value
Note: The above method must have parentheses
2.2 Random number method Random()
The random() method can return a decimal at random, and its value range is [0, 1), left closed and right open 0 <= x < 1
Example: Get a random integer between two numbers, including the two numbers.
The code is as follows (example):
function getRandom(min, max) {<!-- --> return Math. floor(Math. random() * (max - min + 1)) + min; }
3. Date object
3.1 Date Overview
- The Date object is different from the Math object, it is a constructor, so we need to instantiate it before using it
- Date instance for working with dates and times
3.2 Use of Date() method
Getting the current time must be instantiated
Code example:
var now = new Date(); console. log(now);
Arguments to the Date() constructor
If there is a time in the parentheses, the time in the parameter is returned.
For example, the date format string is ‘2019-5-1’, which can be written as
new Date('2019-5-1') or new Date('2019/5/1')
If Date() does not write parameters, it will return the current time
If the parameter is written in Date(), it will return the time entered in the brackets
3.3 Date formatting
Method name | Description | Code |
---|---|---|
getFullYear() | Get the current year | Date.getFullYear() |
getMonth() | Get the month of the month, (0-11) | Date.getMonth() |
getDate() | Get the date of the day | Date.getDate() |
getDay() | Get the day of the week, (Sunday: 0 to Saturday: 6) | Date.getDay() |
getHours() | Get the current hour | Date.getHours() |
getMinutes() | Get the current minute | Date.getMinutes() |
getSeconds() | Get the current number of seconds | Date.getSeconds() |
3.4 Get the total milliseconds of the date
Date object is based on the number of milliseconds since January 1, 1970 (UTC)
Code example:
// Instantiate the Date object var now = new Date(); // 1. Used to get the original value of the object console. log(date. valueOf()) console. log(date. getTime()) // 2. Simple writing can do this var now = + new Date(); // 3. The method provided in HTML5 has compatibility issues var now = Date. now();
4. Array object
4.1 Creation of array objects
Literal way:
var arr = [10,4,2,19,7,4];
new Array() method:
var arr = new Array();
4.2 Check if it is an array
- The instanceof operator can determine whether an object belongs to a certain type
- Array.isArray() is used to judge whether an object is an array, isArray() is a method provided in HTML5
Code example:
var arr = [1, 23]; var obj = {<!-- -->}; console.log(arr instanceof Array); // true console.log(obj instanceof Array); // false console.log(Array.isArray(arr)); // true console.log(Array.isArray(obj)); // false
4.3 Methods of adding and deleting array elements
Method name | Description | Return value |
---|---|---|
push(parameter 1, parameter 2,…) | Adding one or more elements at the end of the array will modify the original array | Return the new length of the array |
pop() | Deleting the last element of the element will modify the original array | Return the deleted element |
unshift(parameter 1,parameter 2,…) | adding one or more elements at the beginning of the array will modify the original array | return the new array Length |
shift() | Deleting the first element of the array will modify the original array | Return the value of the deleted element |
4.4 Array sorting
Method name | Description | Whether to modify the original array |
---|---|---|
reverse | Reversing the order of the elements in the array | will change the original array and return a new array |
sort | Sorting the elements of the array | will change the original array and return a new array |
Code example:
var arr = [1, 64, 9, 6]; arr. sort(function(a, b) {<!-- --> return b - a; // descending order // return a - b; // ascending order });
4.5 Array index method
Method name | Description | Return value |
---|---|---|
indexOf() | Find the first index of a given element in the array | If it exists, return the index number, otherwise, return -1 |
lastIndexOf() | Find the last index of a given element in the array | If it exists, return the index number, otherwise, return -1 |
4.6 Convert array to string
Method name | Description | Return value |
---|---|---|
toString() | Convert the array into a string, comma separated each element | Return a string |
Join (“delimiter”) | Convert all elements in the array into a string | Return a string |
4.7 Other methods
Method name | Description | Return value |
---|---|---|
concat() | Connects two or more arrays without affecting the original array | returns a new array |
slice () | Array interception slice(begin,end) | Return a string |
splice() | array deletion, splice (start position, number of deletions), affects the original array |
returns a new array |
Method name | Description |
---|---|
indexOf(‘The character to look for ‘,’The position to start searching’) | Returns the position of the specified content in the string, if not found, returns -1 |
lastIndexOf(‘to The searched character’) | Search from the back to the front, return the first found position, return -1 if not found |
5.3 Return characters according to position
Method name | Description | Use |
---|---|---|
charAt(index) | Returns the character at the specified position (index is the index value of the string) | str.charAt(index) |
charCodeAt(index) | Returns the ASCII code of the character at the specified position | string.charCodeAt(index) |
str[inde] | Returns the character at the specified position (index is the index value of the string) | stri[index] HTML, IE8 + support |
5.4 String manipulation methods
Method name | Description |
---|---|
replace(‘replaced character’ ,’To replace the character’) | Replace the corresponding character in the string |
split(‘delimiter’) | Split String, split the string into an array, return a new array |
concat(parameter 1,parameter 2,…) | Connect two or more string, same effect as +, recommend + |
substr(start,length) | to intercept the string, starting from start, intercepting length characters |
slice(start,end) | Intercept the string, starting from start and ending at end does not contain the end character |
substring(start,end) | Intercept the string, starting from start and ending at end does not contain the end character, and does not support negative numbers |
toUpperCase() | Convert all characters of the string to uppercase |
toLowerCase() | Convert all characters of the string to lowercase |