1 – Built-in objects

1.1 Built-in Objects

JavaScript objects are divided into three types: custom objects, built-in objects, and browser objects. The first two types of objects are basic JS content and belong to ECMAScript. The third browser object belongs to JS unique, JS API explains the built-in object refers to some objects of JS language, these objects for developers to use, and provide some common or the most basic and necessary functions (attributes and methods), the biggest advantage of the built-in object is to help us develop quickly

JavaScript provides several built-in objects: Math, Date, Array, String, and so on

1.2 check the document

Find documentation: To learn the use of a built-in object, just learn the use of its common members, we can learn by looking up documentation, we can query through MDN/W3C. The Mozilla Developer Network (MDN) provides information about Open Web technologies, including HTML, CSS, and apis for the World Wide Web and HTML5 applications. MDN:developer.mozilla.org/zh-CN/

1.3 Math object

The Math object is not a constructor; it has properties and methods of mathematical constants and functions. Math related operations (absolute values, roundings, maxima, and so on) can use members of Math.

Property, method name function
Math.PI PI
Math.floor() Take down the whole
Math.ceil() Take up the whole
Math.round() Note that the nearest rounded version is -3.5 and the result is -3
Math.abs() The absolute value
Math.max()/Math.min() Find the maximum and minimum
Math.random() Gets a random value in the range [0,1)

Note: The above method must be used with parentheses

Gets a random integer in the specified range:

function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min; 
}
Copy the code

1.4 Date Object

The Date object, unlike the Math object, is a constructor, so you need to instantiate it before you can use its specific methods and properties. The Date instance is used to handle dates and times

  • Instantiate a Date object with Date

    • To get the current time, you must instantiate:
    var now = new Date(a);Copy the code
    • Gets a date object at the specified time
    var future = new Date('2019/5/1');
    Copy the code

    Note: If no arguments are passed when creating the instance, the resulting date object is the date object corresponding to the current time

  • Use the methods and properties of the Date instance

  • Get the total number of millimeters from the Date instance

    • The total number of milliseconds

      Based on the number of milliseconds from January 1, 1970 (UTC)

    • Gets the total number of milliseconds

      // Instantiate the Date object
      var now = new Date(a);// 1. Get the original value of the object
      console.log(date.valueOf())	
      console.log(date.getTime())	
      // 2
      var now = + new Date(a);// 3. The method provided in HTML5 has compatibility issues
      var now = Date.now();
      Copy the code

1.5 Array Objects

There are two ways to create arrays

  • Literal mode

    • Example code is as follows:

      var arr = [1."test".true];
      Copy the code
  • new Array()

    • Example code is as follows:

      var arr = new Array();
      Copy the code

      Note: ArR creates an empty Array. If you need to use the Array constructor to create a non-empty Array, you can pass in parameters when creating the Array

      Parameter transfer rules are as follows:

      • If only one argument is passed, it specifies the length of the array

      • If more than one parameter is passed, the parameter is called the element of the array

Check if it is an array

  • The instanceof operator

    • Instanceof determines whether an object is an instanceof a constructor

      var arr = [1.23];
      var obj = {};
      console.log(arr instanceof Array); // true
      console.log(obj instanceof Array); // false
      Copy the code
  • Array.isArray()

    • Array.isarray () is used to determine whether an object is an Array. IsArray () is a method provided in HTML5

      var arr = [1.23];
      var obj = {};
      console.log(Array.isArray(arr));   // true
      console.log(Array.isArray(obj));   // false
      Copy the code

Adds a method to delete an array element

  • There are methods for adding and deleting elements in an array. The following table lists some methods

Note: Push and unshift are methods for adding elements; Pop and Shift are methods to delete elements

Sort an array

  • There are some methods to sort the array itself

Note: The sort method requires parameters to set ascending and descending sort

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

Array index method

  • There are methods to get the index value of the specified element in the array

Arrays are converted to strings

  • There are methods for converting an array to a string

Note: If the join method takes no arguments, it concatenates elements according to “,”

Other methods

  • There are other things that you can do in the array that you can look up after class

1.6 String Objects

Basic packing type

To make it easier to manipulate basic data types, JavaScript also provides three special reference types: String, Number, and Boolean.

A primitive wrapper type wraps a simple data type into a complex data type, so that the primitive data type has properties and methods.

// What's wrong with the following code?
var str = 'andy';
console.log(str.length);
Copy the code

Basic data types don’t have properties and methods, whereas objects have properties and methods, but this code can execute because

Js wraps basic datatypes into complex datatypes as follows:

// 1. Generate temporary variables to wrap simple types into complex data types
var temp = new String('andy');
// 2. Assign to the character variable we declared
str = temp;
// 3. Destroy temporary variables
temp = null;
Copy the code

Immutability of a string

The value inside is immutable, although it looks like it can change the content, but in fact, the address is changed, and a new memory space is opened in memory.

When reassigning a value to a string variable, the string stored before the variable is not modified. Reassigning a value to the string in memory will create space in memory. This feature is the immutable character of the string. Due to the immutable nature of strings, there are efficiency issues when concatenating large numbers of strings

Return position by character

A string can be manipulated by calling partial methods using the basic wrapper type. Here is a method that returns the position of the specified character:

Example: Find the positions and times of all o occurrences in the string “abcoefoxyozzopp”

  1. So let’s look for the first o
  2. IndexOf then returns a result other than -1 to continue the search
  3. Since indexOf can only find the first one, subsequent lookups continue by incrementing the current index with the second argument

Returns characters based on position

A string can be manipulated by calling partial methods using the basic wrapper type. The following is a string that returns characters at the specified position by position:

In the above method, the charCodeAt method returns the ASCII code corresponding to the character at the specified position. The ASCII code comparison table is as follows:

Example: Determine the most frequently occurring character in a string ‘abcoefoxyozzopp’ and count the number of occurrences

  1. Core algorithm: Use charAt() to traverse the string

  2. Stores each character to the object, 1 if it doesn’t have the property, +1 if it does

  3. Iterates through the object to get the maximum value and the character

    Note: During traversal, each character in the string is stored in the object as an attribute. The corresponding attribute value is the number of occurrences of this character

String manipulation methods

A string can be manipulated by calling partial methods through the primitive wrapper type. Here are some of the partial manipulation methods:

The replace () method

The replace() method is used to replace some characters with others in a string, using the following format:

String.replace (the string to be replaced, the string to be replaced with);Copy the code

The split () method

The split() method is used to split strings into arrays. After the shard is complete, a new array is returned.

Its use format is as follows:

String.split(" split character ")Copy the code

2 – Simple and complex data types

2.1 Simple data types

Simple types (basic data types, value types) : The value itself is stored in the variable during storage, including String, number, Boolean, undefined, null

2.2 Complex data types

Complex data types (reference types) : Only addresses (references) are stored in variables during storage. Objects (system objects, custom objects) created by the new keyword, such as Object, Array, Date, etc.

2.3 the stack

  • Stack space allocation difference:

1. Stack (operating system) : the operating system automatically distributes and releases the parameter values and local variable values of the storage function. It operates like a stack in a data structure;

Simple data types are stored on a stack

2, heap (operating system) : store complex types (objects), generally allocated by the programmer to release, if the programmer does not release, by the garbage collection mechanism to recover.

  • Storage of simple data types

    The data of a value type variable is stored directly in the variable (stack space)

  • How complex data types are stored

    Reference type variables (stack space) store addresses, and real object instances are stored in heap space

2.4 Simple Type Parameter Transmission

A function parameter can also be considered a variable. When we pass a value variable as a parameter to a function parameter, we are actually copying the value of the variable in the stack space to the parameter, so that any changes made to the parameter inside the method will not affect the external variable.

function fn(a) {
    a++;
    console.log(a); 
}
var x = 10;
fn(x);
consoleLog (x);Copy the code

The running results are as follows:

2.5 Parameter Transfer for Complex Data Types

A parameter of a function can also be treated as a variable. When we pass a reference to a type variable to a parameter, we are actually copying the stack address of the variable to the parameter. The parameter and the argument actually hold the same heap address, so we operate on the same object.

function Person(name) {
    this.name = name;
}
function f1(x) { // x = p
    console.log(x.name); // 2. What is this output?
    x.name = Jacky Cheung;
    console.log(x.name); // 3. What is this output?
}
var p = new Person("Andy Lau");
console.log(p.name);    // 1. What is this output?
f1(p);
console.log(p.name);    // 4. What is this output?
Copy the code

The running results are as follows: