Important objects

In JS, all reference types are objects, and functions are reference types and therefore objects. Important objects naturally include important constructors, namely classes, because JS classes are realized by functions

Packing type

let a = new String("word");
console.log(typeof a); // object
Copy the code
  1. What are the basic data types? String type/numeric type/Boolean type/null type/undefined type/symbol

  2. The data of primitive data types created by literals is constant

  3. Constant features and attention points

    • Constants cannot be modified

      let str = "abc";
      str[1] = "m";
      console.log(str); // abc
      Copy the code
    • Each ‘modification’ or concatenation generates a new one

      let str = "abc";
      let newStr = str.replace("b"."m");
      console.log(str); // abc
      console.log(newStr); // amc
      Copy the code
  4. Basic type characteristics

    • There are no properties or methods

      let str = "lnj";
      str.age = 34;
      str.say = function () {
          console.log("hello");
      }
      console.log(str.age); // undefined
      str.say(); // str.say is not a function
      Copy the code
  5. Characteristics of object types

    • There are properties and methods
  6. Previously, you could access properties and methods of primitive data types because

    • At runtime, the system automatically wraps the basic data types into object types

    • String() / Number() / Boolean()

      let str = "www.it666.com";
      // let str = new String(str); // The underlying object is not the string base type, but the new object of the string class is calling methods
      console.log(str.length);
      str.split(".");
      Copy the code

The three object

JavaScript provides three built-in objects:

  1. Local object
  2. Built-in objects
    1. Global
    2. Math
    3. JSON
  3. The host object

What is a host?

  • Host is the JavaScript runtime environment. Js can be run in a browser or on a server

Local object

  • Host-independent objects that exist in both the browser and the server
  • Objects produced by classes (constructors) defined in the ECMAScript standard
  • In the process of use we need to manually create new
  • For example, Boolean, Number, String, Array, Function, Object, Date, RegExp, and so on

Built-in objects

  • Host-independent objects that exist in both the browser and the server
  • JavaScript has already created the object for us.
  • We don’t need to create new manually during use
  • For example, Global, Math, JSON, and Date

The host object

  • For JS embedded in a web page, the host object is the browser, so the host object is the object provided by the browser
  • Contains: Window, Document, etc
  • All DOM and BOM objects belong to the host object, and only the browser host provides the DOM and BOM

Built-in objects

Math

Math. The floor () | integer downward

Math. The ceil () | integer upwards

Math. The round () | rounded

Math. Abs () | absolute value

Math. The random () | to generate random number

Classic apps (not easy!) :

// Requirement: generate a random number from 1 to 10
function getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; // Contain maximum value, contain minimum value
}
let value = getRandomIntInclusive(0.10);
console.log(value);

/* Analyze a wave: 10-0 + 1 = 11 Math.random() is the interval in which math.random () * 11 produces the average probability density from zero to eleven. The eleven numbers divide into ten segments, each of which is equally likely. Because the probability of random picking an integer is close to 0 */
Copy the code

Date

The base is held
  1. There’s a standard format for the object that comes out of new to get the current time and we don’t usually use that default format

    let date = new Date(a);console.log(date); / / the T10:2020-11-07 joyfully. 483 z
    console.log(typeof date); // object 
    Copy the code
  2. Gets the current time in milliseconds from January 1, 1970 (UTC)

    • Methods a

      console.log(Date.now());
      Copy the code
    • Method 2

      // The.valueof is called at the bottom when the journal object is evaluated
      let date = new Date(a);console.log(date.valueOf());
      Copy the code
  3. Create a specified time

    • Methods a

      // year month day hour minute second
      let date = new Date("The 2020-11-07 10:10:34");
      console.log(date);
      Copy the code
    • When creating a specified time, if the month is passed in separately, an extra month will be added

      let date = new Date(2020.10.07.9.8.7);
      console.log(date);
      Copy the code
  4. Gets the specified time year month day hour minute second

    	x 1let date = new Date(a); 2console.log(date.getFullYear());34// Note that; 5console.log(date.getMonth() + 1); 67console.log(date.getDate()); Log (date.gethours ()); 9console.log(date.getMinutes()); 10console.log(date.getSeconds()); 13console.log(date.getmilliseconds ()); 14console.log(date.getDay()); // Get the number of js
    Copy the code
  5. Time formatting

    let date = new Date(a);let res = formatDate(date);
    
    console.log(res); / / the 2020-11-7 18:30:46
    
    function formatDate(date) {
        return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
    }
    Copy the code
Calculation time difference

Take the millisecond value of the two points in time and calculate the difference of days, hours, minutes and seconds

	 13let curDate = new Date("2020-4-19 22:30:20"); 3let curDate = new Date("2020-4-19 22:30:20"); 4let remDate = new Date("2020-4-30 00:00:00"); 1. Get the difference between the two times (ms)6let differTime = remDate - curDate; 7// let differTime = remDate.valueOf() - curDate.valueOf(); Get the difference between the two times (seconds)9let differSecond = differTime / 1000; 11let day = math.floor (differSecond/(60 * 60 * 24)); 12console.log(day); 13// 4. Total number of seconds/hour using the difference % 24; 14let hour = Math.floor(differSecond / (60 * 60) % 24); 15console.log(hour); 16// 5. The total number of seconds/min using the difference % 60; 17let minute = Math.floor(differSecond / 60 % 60); 18console.log(minute); 20. Let second = differSecond % 60; 21console.log(second); // It is not appropriate to use parseInt (IDE warns), because parseInt should accept strings, even though it is a weakly typed language, it is best to separate js classes from what they do
Copy the code