*JavaScript Fundamentals 6 Built-in Objects·Stack·Passing Parameters

1-Built-in objects

1.1 Built-in objects

​ 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 is unique to JS, and JS API explains built-in objects. It refers to some objects that come with the JS language. These objects are for developers to use and provide some commonly used or the most basic and necessary functions (attributes and methods). The biggest advantage of built-in objects is to help us develop quickly

​ JavaScript provides multiple built-in objects: Math, Date, Array, String, etc.

1.2 Check documents

​ Finding documents: To learn the use of a built-in object, as long as you learn to use its common members, we can learn by looking up the document, and we can query through MDN/W3C.
​ The Mozilla Developer Network (MDN) provides information about Open Web, including HTML, CSS, and APIs for the World Wide Web and HTML5 applications.
​ MDN: https://developer.mozilla.org/zh-CN/

1.3 Math object

​ The Math object is not a constructor, it has the properties and methods of mathematical constants and functions. Math-related operations (absolute value, rounding, maximum value, etc.) can use the members in Math.

Property, method name Features
Math.PI PI
Math.floor() Round down
Math.ceil() Improvement arrangement
Math.round() The rounded version is rounded to the nearest integer. Note -3.5 The result is -3
Math.abs() Absolute value
Math.max()/Math.min() Find the maximum and minimum
Math.random() Get a random value in the range (0,1)

​ Note: The above method must be used with parentheses

Acquiring random integer within the specified range :

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

1.4 Date Object

​ The Date object is not the same as the Math object. Date is a constructor, so you need to instantiate it before you can use the specific methods and properties. Date instance is used to process date and time

  • Use Date to instantiate a date object

    • Get the current time must be instantiated:
    var now = new Date();
    
    • Get the date object at the specified time
    var future = new Date('2019/5/1');
    

    Note: If no parameters are passed in when creating the instance, the date object will be the date object corresponding to the current time

  • Use the methods and properties of Date instances

Insert picture description here

  • Get the total number of millimeters through the Date instance

    • The meaning of total milliseconds

      ​ Based on the number of milliseconds since January 1, 1970 (Coordinated Universal Time)

    • Get the total number of milliseconds

      // 实例化Date对象
      var now = new Date();
      // 1. 用于获取对象的原始值
      console.log(date.valueOf())	
      console.log(date.getTime())	
      // 2. 简单写可以这么做
      var now = + new Date();			
      // 3. HTML5中提供的方法,有兼容性问题
      var now = Date.now();
      

1.5 Array Object

Two ways to create an array

  • Literal way

    • The sample code is as follows:

      var arr = [1,"test",true];
      
  • new Array()

    • The sample code is as follows:

      var arr = new Array();
      

      ​ Note: In the above code, arr creates an empty array. If you need to use the constructor Array to create a non-empty array, you can pass in parameters when creating the array

      ​ The parameter passing rules are as follows:

      • If only one parameter is passed in, the parameter specifies the length of the array

      • If multiple parameters are passed in, the parameters are called elements of the array

Check whether it is an array

  • instanceof operator

    • instanceof can determine whether an object is an instance of a constructor

      var arr = [1, 23];
      var obj = {
              
              };
      console.log(arr instanceof Array); // true
      console.log(obj instanceof Array); // false
      
  • 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
      

Add a method to delete array elements

  • There are methods to add and delete elements in the array. Some methods are as follows

Insert picture description here

Note: push and unshift are methods for adding elements; pop and shift are methods for deleting elements

Array sort

  • There are methods for sorting the array itself in the array, some of the methods are as follows

Insert picture description here

Note: The sort method needs to pass in parameters to set the ascending and descending order

  • If "function(a,b){ return ab;}" is passed in, it will be in ascending order
  • If "function(a,b){ return ba;}" is passed in, it will be in descending order

Array index method

  • There are methods to get the index value of the specified element of the array in the array, some of the methods are as follows

Insert picture description here

Convert array to string

  • There are methods to convert the array into a string in the array, some of the methods are as follows

Insert picture description here

Note: If the join method does not pass in parameters, the elements will be spliced ​​according to ","

Other methods

  • There are other operation methods in the array, students can check and learn by themselves after class

Insert picture description here

1.6 String Object

Basic packaging type

​ In order to facilitate the operation of basic data types, JavaScript also provides three special reference types: String, Number and Boolean.

​ The basic package type is to package simple data types into complex data types, so that the basic data types have attributes and methods.

// 下面代码有什么问题?
var str = 'andy';
console.log(str.length);

​ According to reason, the basic data type does not have attributes and methods, but objects have attributes and methods, but the above code can be executed. This is because

​ js will package basic data types into complex data types, and the execution process is as follows:

// 1. 生成临时变量,把简单类型包装为复杂数据类型
var temp = new String('andy');
// 2. 赋值给我们声明的字符变量
str = temp;
// 3. 销毁临时变量
temp = null;

Immutable string

​ refers to the immutable value inside. Although it seems that the content can be changed, the address is actually changed, and a new memory space is opened in the memory.

​ When re-assigning a string variable, the previously saved string of the variable will not be modified, and re-assigning the string in the memory will open up space in the memory again. This feature is that the string is immutable.
​ Due to the immutability of strings , there will be efficiency problems when concatenating a large number of strings

Return position based on character

​ Strings can call some methods to manipulate strings through the basic packaging types. The following is the method to return the position of the specified character:

Insert picture description here

​ Case: Find the position and number of occurrences of all o in the string "abcoefoxyozzopp"

  1. First find the position where the first o appears
  2. Then as long as the result returned by indexOf is not -1, continue to search backwards
  3. Because indexOf can only find the first one, the subsequent search uses the second parameter, and the current index is incremented by 1, so as to continue the search

Return characters based on position

​ Strings can call some methods to manipulate strings through basic packaging types. The following is to return the characters at the specified position according to the position:

Insert picture description here

​ 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:

Insert picture description here

​ Case: Determine the character with the most occurrences in a string'abcoefoxyozzopp' and count its times

  1. Core algorithm: use charAt() to traverse this string

  2. Store each character in the object, if the object does not have this attribute, it will be 1, if it exists, it will be +1

  3. Traverse the object, get the maximum value and the character

    ​ Note: In the process of traversal, each character in the string is stored in the object as the attribute of the object, and the corresponding attribute value is the number of times the character appears

String manipulation method

​ Strings can call some methods to manipulate strings through basic packaging types. The following are some of the operation methods:

Insert picture description here

replace() method

The replace() method is used to replace some characters with other characters in a string, and its format is as follows:

字符串.replace(被替换的字符串, 要替换为的字符串);

split() method

​ The split() method is used to split a string, it can split a string into an array. After the segmentation is completed, a new array is returned.

​ Its usage format is as follows:

字符串.split("分割字符")

2-Simple data type and complex data type

2.1 Simple data types

Simple type ( basic data types , value types ): when stored in the storage variable is the value itself, including string, number, boolean, undefined, null

2.2 Complex data types

Complex data types (reference types) : stored in the variable address memory only (reference) object is created by the new key (system objects, custom objects), such as Object, Array, Date and the like;

2.3 Stack

  • The difference in stack space allocation:

1. Stack (operating system): The operating system automatically allocates and releases the parameter values ​​of the stored function, the values ​​of local variables, etc. Its operation mode is similar to the stack in the data structure;

Simple data types are stored in the stack

2. Heap (operating system): Stores complex types (objects), which are generally allocated and released by the programmer. If the programmer does not release, they are reclaimed by the garbage collection mechanism.

Insert picture description here

  • Storage of simple data types

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

Insert picture description here

  • Storage of complex data types

    ​ Reference type variables (stack space) are stored in the address, the real object instance is stored in the heap space

Insert picture description here

2.4 Simple type parameter transfer

​ The formal parameter of a function can also be regarded as a variable. When we pass a value type variable as a parameter to the formal parameter of the function, we actually copy the value of the variable in the stack space to the formal parameter, then Any modification to the formal parameters inside the method will not affect the external variables.

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

​ The results of the operation are as follows:

2.5 Passing parameters of complex data types

​ The formal parameter of a function can also be regarded as a variable. When we pass a reference type variable to a formal parameter, we actually copy the heap address of the variable stored in the stack space to the formal parameter, and the formal parameter and the actual parameter are actually stored Is the same heap address, so the operation is the same object.

function Person(name) {
    this.name = name;
}
function f1(x) { // x = p
    console.log(x.name); // 2. 这个输出什么 ?    
    x.name = "张学友";
    console.log(x.name); // 3. 这个输出什么 ?    
}
var p = new Person("刘德华");
console.log(p.name);    // 1. 这个输出什么 ?   
f1(p);
console.log(p.name);    // 4. 这个输出什么 ?  

​ The results of the operation are as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48116269/article/details/107956655