Chapter 5 basic reference types

Date Date function

  1. To include only dates, use date.parse ()

    let someDate = new Date(Date.parse("May 23, 2019"));
    
    let someDate = new Date("May 23, 2019"); // Equivalent to the code above
    Copy the code
  2. For milliseconds, date.utc ().ps: the month must start at 0, which is the month.

    // May 5, 2005, 5:55 PM GMT
    let allFives = new Date(Date.UTC(2005.4.5.17.55.55));
    
    // 5:55.55 PM local time on 5 May 2005
    let allFives = new Date(2005.4.5.17.55.55);
    Copy the code

    Explicit calls use GET time, implicit calls use local time,

  3. The Date type has several methods for formatting dates, all of which return strings: toDateString() displays the day of the week, month, day, and year of the Date (the format is implementation-specific); ToTimeString () displays the hours, minutes, seconds, and time zone in the date (format is implementation-specific);

RegExp Regular expression

  1. Flag for matching pattern: G: global pattern, which looks for the entire content of the string rather than ending with the first match. I: Case insensitive, which means that the case of pattern and string is ignored when searching for matches. M: Multi-line mode, which indicates that the search will continue until the end of a line of text. Y: Adhesion mode, which only looks for strings starting from and after lastIndex. U: Unicode mode, enabling Unicode matching. S: indicates the metacharacter in dotAll mode. Matches any character (including \n or \r).

    // Matches all "at" in the string
    let pattern1 = /at/g;
    
    // Matches the first "bat" or "cat", ignoring case
    let pattern2 = /[bc]at/i;
    
    // Matches all three-character combinations ending in "at", regardless of case
    let pattern3 = /.at/gi;
    Copy the code
  2. Exec (), which is mainly used with capture groups; Test (), which takes a string argument; I don’t use them very much.

Raw value wrapper type

  1. To facilitate manipulation of raw values, ECMAScript provides three special reference types: Boolean, Number, and String, which become a reference value (object) when created by new.

  2. Every time a method or property of a primitive value is used, an object of the corresponding primitive wrapper type is created in the background, exposing the various methods that operate on the primitive value. For example:

    let s1 = "some text";
    let s2 = s1.substring(2);
    Copy the code

    In this case, s1 is a variable containing a string, which is a raw value. The second line immediately calls the substring() method on S1 and stores the result in S2. We know that primitive values themselves are not objects, so logically there should be no methods. And in fact this example works exactly as expected. This is because there is a lot of processing going on in the background to make this happen.

  3. Specifically, when the second line accesses S1, it does so in read mode, that is, it reads the value that the variable holds from memory. Any time a String value is accessed in read mode, the following three steps are performed in the background: (1) Create an instance of String; (2) call a specific method on the instance; (3) Destroy the instance.

    // Think of these three steps as executing the following three lines of ECMAScript code
    let s1 = new String("some text");
    let s2 = s1.substring(2);
    s1 = null // This will destroy the new object
    Copy the code

    This behavior allows the original value to have the behavior of the object. For booleans and numeric values, the above three steps also happen in the background, but use Boolean and Number to wrap the type

  4. The main difference between reference types and raw value wrapper types is the life cycle of the object. After a reference type is instantiated with new, the resulting instance is destroyed when it leaves scope, and the automatically created raw value wrapper object only exists for the execution of the line of code that accessed it. This means that attributes and methods cannot be added to raw values at run time.

    let s1 = "some text";
    s1.color = "red";
    
    console.log(s1.color); // undefined
    Copy the code

    The second line here attempts to add a color attribute to the string s1. However, when the third line of code accesses the color property, it is gone. The reason is that the second line of code creates a temporary String object when it runs, and when the third line of code executes, the object has already been destroyed. In fact, the third line of code creates its own String object here, but this object has no color attribute.

  • Boolean, Number, and String are three special reference types that can act as primitive value wrapper types that refer to values (objects). However, it is not recommended to explicitly create instances of primitive wrapper types, because developers can easily confuse whether they are primitive or reference values.

Singleton built-in object

Global

  1. EncodeURI () and encodeURIComponent(). Need to use in-depth understanding
  2. The eval (). This method is a complete ECMAScript interpreter that takes a single parameter, an ECMAScript (JavaScript) string to be executed.Learn more when you need to use it
    Two lines of code are equivalent
    eval("console.log('hi')");
    console.log("hi"); 
    Copy the code

Math

  1. Take the most worthwhile method. The min() and Max () methods are used to determine the minimum and maximum values in a set of values. Both methods take any number of arguments

    let max = Math.max(3.54.32.16);
    console.log(max); / / 54
    
    let min = Math.min(3.54.32.16);
    console.log(min); / / 3
    
    let values = [1.2.3.4.5.6.7.8];
    let max = Math.max(... val);/ / 8
    Copy the code
  2. Rounding method. The four methods for rounding small values to integers: math.ceil (), math.floor (), math.round (), and math.fround () math.ceil () methods always round up to the nearest integer. The math.floor () method always rounds down to the nearest integer. The math.round () method performs rounding. The math.fround () method returns the closest single-precision (32-bit) floating-point representation of the value.

    // the math.ceil () method always rounds up to the nearest integer.
    console.log(Math.ceil(25.9)); / / 26
    console.log(Math.ceil(25.5)); / / 26
    console.log(Math.ceil(25.1)); / / 26
    
    // the math.floor () method always rounds down to the nearest integer.
    console.log(Math.floor(25.9)); / / 25
    console.log(Math.floor(25.5)); / / 25
    console.log(Math.floor(25.1)); / / 25
    
    // math.round () performs rounding
    console.log(Math.round(25.9)); / / 26
    console.log(Math.round(25.5)); / / 26
    console.log(Math.round(25.1)); / / 25
    
    The math.fround () method returns the closest single-precision (32-bit) floating-point representation of the value
    console.log(Math.fround(0.4)); / / 0.4000000059604645
    console.log(Math.fround(0.5)); / / 0.5
    console.log(Math.fround(25.9)); / / 25.899999618530273
    Copy the code
  3. Gets a random number. The math.random () method returns a random number in the range 0 to 1, including 0 but not 1

    // Select a number from 1 to 10 randomly
    let num = Math.floor(Math.random() * 10 + 1);
    Copy the code

summary

Objects in JavaScript are called reference values, and several built-in reference types can be used to create objects of a specific type.

  • Reference values are similar to classes in traditional object-oriented programming languages, but the implementation is different.
  • The Date type provides information about the Date and time, including the current Date, time, and associated calculations.
  • The RegExp type is an ECMAScript interface that supports regular expressions and provides most of the basic and some advanced regular expression functionality.

One of the things that makes JavaScript unique is that functions are actually instances of type Function, which means functions are also objects. Because functions are objects, they also have methods that can be used to enhance their capabilities.

Because of the primitive value wrapper type, primitive values in JavaScript can be used as objects. There are three primitive value wrapper types: Boolean, Number, and String. They all have the following characteristics.

  • Each wrapper type maps to a primitive type of the same name.
  • When raw values are accessed in read mode, an object of raw value wrapper type is instantiated in the background, with which the corresponding data can be manipulated.
  • After the statement involving the original value completes, the wrapped object is destroyed.

When the code starts executing, there are two built-in objects in the Global context: Global and Math. Of these, the Global object is not directly accessible in most ECMAScript implementations. However, the browser implements it as a Window object. All Global variables and functions are properties of the Global object. The Math object contains properties and methods that aid in complex calculations.