Built-in objects

There are three types of objects in JS: custom objects, built-in objects, and browser objects. The first two are JS based and belong to ECMAScript. Browser objects are unique to JS and will be covered in Web API later.

What is included in built-in exclusivity

The biggest advantage of built-in objects is that they help developers become more productive

How to learn the use of an object

Learning JS built-in object, need to query the document, in learning a built-in object, only need to learn how to use its internal members according to the document.

The Math object

  1. PI: PI 3.14159

Commonly used method

  1. Abs () : Finds the absolute value of a number
  2. Ceil () : round up, returns itself if the number is an integer in nature, returns an integer 1 greater than the integer part of the decimal if it is a decimal, and removes the decimal part if it is negative
  3. Floor () : round down, returning itself if the number is an integer in nature, returning the fractional integer part if it is a decimal, and the negative integer 1 less than the integer part if it is negative
  4. Round () : the nearest round is -3.5. The result is -3
  5. Max () and min() : find the maximum or minimum value
  6. Random () : gets a random number between [0-1] 0=

How to handle a random integer:

console.log(Math.round(Math.random()*10));

A random integer within a specified range:

// Specify a range integer that takes a random integer and requires both integers to be included. Function randomNum(min, Max){return math.floor (math.random ()*(max-min+1)+min); } the console. The log (randomNum (5, 12)); 1Copy the code

The Date object

Unlike Math, a Date object is a constructor, so you must instantiate it (call the constructor) before you can use its properties and methods. Date objects are primarily used to process dates and times

There are actually four types of Date object constructors

Instantiate the Date

Var now=new Date();

2. Obtain the date object at the specified time

Var now = new Date (" 2021,10,1 ");

When creating a time, write parameters in the second format. The parameter format can be “2020,11,18” or “2020/11/18” in string format

Or you could just use the date as an argument, so you could just write 220,11,18

methods

  1. GetFullYear () : Gets the year part of the current object
  2. GetMonth () : Gets the month of the current object (0-11)
  3. GetDay () : Gets the number of days (0-6) of the current object, where 0 is the day of the week
  4. GetDate () : Gets the number of days of the current object
  5. GetHours () : Gets the hours of the current time 24 hours
  6. GetMinutes () : Gets the number of minutes of the current time
  7. GetSeconds () : Gets the number of seconds of the current time
  8. GetMilliseconds () : Gets the number of milliseconds of the current time
  9. GetTime () : Gets the number of milliseconds from 1970-1-1(Utc)

Conclusion:

Methods that start with GET in a method are generally methods that get some data

Methods that start with set are usually methods that set some data

Methods that start with to are generally methods that convert the data type to the target type

The Array object

Array object The global object used to construct an Array

Create an array

  1. Literal mode

  2. New Array () method

     var arr=new Array();
    Copy the code

Note: The Array created above is an empty Array. If you want to create a non-empty Array using an Array object, you need to instantiate the Array object with a realistic parameter

  1. If you pass in only one argument, you specify that the array is 3 in length and all elements are undefined

  2. If more than one parameter is passed in, those parameters act as elements of the array

An array of classification

  1. One-dimensional arrays: linear

  2. Two-dimensional array: face type

  3. Multidimensional arrays: size

    Var arr1=[1,2,3,4,5]; console.log(arr1); Var arr2=[[1,2,3],[2,3],[1,3,2]]; console.log(arr2); / / create a three dimensional array var arr3 = [[[1, 2], [2, 3], [1, 3], [1]], [[2, 3], [1, 3], [2]]]. console.log(arr3);Copy the code

Detection array

1. The instanceof operator: determines whether an object is an instanceof a constructor

console.log(arr1 instanceof Array);
Copy the code
  1. The only variable whose equal sign is given by the constructor is true
  2. Array literals are created by default by executing a constructor

2. Array.isarray () is used to determine whether an object is an Array. This method is provided in HTML5

console.log(Array.isArray(num));
Copy the code

attribute

  1. Length: indicates the length of the array

methods

A method that operates on an element

  1. Push (parameter 1, parameter 2…) : Adds one or more elements to the end of an array, returning the length of the added elements
  2. Pop () : Deletes from the last element in the array, reducing the length of the array by 1 each time, returning the data of the deleted element
  3. Unshift (parameter 1, parameter 2…) : Adds one or more elements from the beginning of an array, returning the length of the added elements
  4. Shift () : Deletes from the first element in the array, reducing the length of the array by 1 each time. Returns the deleted element

Sort an array

  1. Reverse () : Reverses the order of the elements in an array, without arguments, and returns a new array

  2. Sort () : Sort the elements of an array. This method changes the order of the elements in the original array and returns a new array

The sort function can provide arguments to set collation rules

  1. Function (a,b){return a-b; } “, is in ascending order

  2. Function (a,b){return b-a; } “, is in descending order

    console.log(arr.sort(function(a,b){return b-a; }));

Indexes and elements

  1. Indexof () : The index in the array that looks for the first occurrence of a given element, or -1 if none exists

  2. Lastindexof () : Finds the first occurrence indexof the specified element, starting with the first element in the array, or -1 if none exists

  3. ToString () : Converts an array to a string, separated by commas

  4. Join () : Converts all elements in an array to a string, concatenated with the specified delimiter, which defaults to a comma if no argument is given

  5. Concat () : Concatenates two or more arrays, returning a new array

  6. Slice (start, end) : Array truncation starts with an index of start, ends with no end index, and returns a truncated new array

  7. Splice (start, count) : Deletes the count elements from the array whose index is start and returns a new array composed of the deleted elements

String object

Basic data types

To facilitate manipulation of basic data types, JS provides three special types: String, Number, and Boolean

Basic data types are called primitive or simple data types, while repackaging a basic data type is called a complex data type so that it can be objectified from a complex data type (basic data types also have concepts of properties and methods)

// var a=5; // var b=new Number(5); // console.log(b); // console.log(typeof a); // The result is Number // console.log(typeof b); // The result is Object // console.log(a==b);Copy the code

Immutability of a string

Immutable here means that the value of a string variable does not change after it is declared. Although it looks like the content of a variable has changed, what it really means is that the address to which the variable points has changed, that is, the space where the new content is stored is a new address space.

String variables alter data:

  1. When you declare a variable to record string data, the name of the variable points to an address that leads to the memory space where the data is recorded
  2. Reassign the variable, and if the new data is the same as the original, the address of the variable name record is the address of the previous creation
  3. If a variable is reassigned and its data is different from the original, the system will create a new space and store the new data first, and then the spatial address stored in the variable name will not be the original address, but the new spatial address
  4. If the original spatial address is no longer recorded then it is reclaimed by the system and the data inside is completely released

So don’t do a lot of concatenation strings in your programming

The nature of strings

A string can be thought of as an array of character types, so its many attributes and methods are used the same way

Commonly used method

  1. CharAt (index) : Returns the character of the specified index position, just as the result of the index method

  2. CharCodeAt (index) : Returns the ASCII character at the specified index position

  3. EndsWith () : Searches for the specified string starting at the specified location, returning true if found otherwise false

  4. Includes (): searches for the specified string starting at the specified location, returning true if found otherwise false

  5. Replace () : Replaces the string specified in the original string with the new string

  6. Substring (start,end) : Intercepts the original string from the start index to the end (no end)

  7. Substr (start,count) : Intercepts the source string by intercepting count characters from the start index

  8. ToUpperCase ()/toLowerCase() : converts letters in a string toUpperCase

Function overloading

define

Multiple functions in an object with the same name and different parameters (number, type, and order of different types) are called function overloading.

And JS functions in the overload to only consider the number of parameters, realization process and other type of language structure also have difference, because in the JS function of parsing is by anonymous functions, function names and variable names are actually the same function, in the same scope within the same variable names allowed only once, Variable names that repeat later are overwritten. So function overloading in JS is controlled primarily by the function arguments property

this.calc=function(){ console.log(arguments); if(arguments.length==0){ return 0; }else if(arguments.length==1){ return arguments[0] } else { var sum=0; for(var i=0; i<arguments.length; i++){ sum+=arguments[i]; } return sum; }}Copy the code

The data type

Data type classification

  1. Simple data types (basic data types and value types) : The value itself is directly stored in the variable during storage, including string, number, Boolean, undefined, and NULL
  2. Complex data types (reference types) : In the storage variable is directly stored only storage data space address (reference), through the new keyword to create objects (system objects, custom objects), such as: Object, Array, Date…

Stack and heap

  1. Stack: Automatically allocated and released by the operating system. Parameter values, local variables and other data stored in the function, value type data is finally stored in the stack
  2. Heap: It is allocated and released by the programmer. If the programmer does not release it, it is automatically released by the garbage collection mechanism of the system. The reference type data is ultimately stored in the heap, but the name of the reference type stores an address in the heap

The advantages and disadvantages:

Value type: Directly stores its data value, which is fast and saves a small amount of data

Reference type: the reference address of the data directly stored in the stack. The data is stored in the heap, which is slow to call and stores a large amount of data

Function and the cords

  1. Value Type Parameter Create objects (system objects, user-defined objects), such as Object, Array, Date…
  2. Reference type parameter transfer