Javascript Advanced Programming Chapter 5 | ch5 | reading notes

Posted by tony-kidsdirect on Mon, 24 Jan 2022 04:14:50 +0100

Basic reference type

Date

  1. Create a Date instance without a given time, and the created object will save the current Date and time.
  2. To create a Date object based on another time, you must pass in its millisecond time representation
    1. Date.parse()
      1. Month / day / year (May 21 / 2022)
      2. May 21, 2022
      3. Day of the week month name day year hour: minute: second time zone (Tue May 23 2022 00:00:00 GMT-0700)
      4. YYYY-MM-DDTHH:mm:ss.sssZ (2019-05-23T00:00:00 is only applicable to ES5 compatible implementations)
    2. Date. UTC (year, month, day, hour, minute, second): returns the millisecond representation of the date

Inherited method

Date formatting method

Date / time component method

RegExp

Original value wrapper type

Boolean

  1. Boolean will override the valueOf() method and return the original value true or false. The toString() method is also overridden when called
let falseObj = new Boolean(false); 

let result = falseObj && true; // All objects are converted to true in bool expressions

console.log(result); // true

  

let falseVal = false;

result = falseVal && true;

console.log(result); // false

Number

  1. Overridden valueOF() toLocalString() toString()
  2. toFixed() 0~20 decimal places
  3. Scientific counting method: toExponential()
  4. Return the most reasonable output: toPrecision()
  5. Is isInteger() an integer

String

Common methods:

  1. normalize() method
  2. String operation method
    1. slice()
    2. substr()
    3. substring()
    4. The first parameter represents the start position of the string, and the second parameter represents the end position
    5. For substr(), the second parameter represents the number of sub characters intercepted
  3. String position method
    1. indexOf(str, idx)
    2. lastIndexOf(str, idx)
    3. Search the incoming string in the string and return the position. If it is not found, return - 1
    4. indexOf find from scratch
    5. lastIndexOf starts at the end
    6. The second parameter means to search from the specified location
  4. String containing method
    1. startsWith(str): whether to start with str
    2. endWith(str): whether to end with str
    3. includes(str): whether to include str
    4. Searches the incoming string and returns a bool value
  5. trim() method: create a copy of the string, delete all the spaces before and after, and then return the result
  6. repeat(times) method: returns the result after splicing the string times
  7. padStart(len, str) and padEnd(len, str) methods
    1. Concatenate the string with the specified str and the string with the length of len
    2. padStart is populated at the beginning
    3. padEnd is filled at the end
  8. String iterator and Deconstruction
let message = 'abc';

let stringIterator = message[Symbol.iterator]();  

console.log(stringIterator.next()); // {value: 'a', done: false}
for (const c of message) console.log(c); // a b c
console.log([...message]); // ['a', 'b', 'c']
  1. String case conversion
    1. toLowerCase()
    2. toLocaleLowerCase()
    3. toUpperCase()
    4. toLocaleUpperCase()
    5. Local: for region specific implementation, if you don't know what language the code involves, finally use local
  2. String template method
    1. match(): essentially the same as the exec() method of RegExp object, it accepts a parameter (regular expression, RegExp object)
    2. search(): always return the first matching position, and the rest are the same as match
    3. replace(): replace
    4. split(): splits the string into an array based on the passed in delimiter
  3. localCompare() method: the locale of the implementation determines how this method compares strings
  4. HTML method

Singleton built-in object

Global

/**

 * encodeURI()

 * encodeURIComponent()

 *

 * decodeURI() Decode only characters encoded by encordURI()

 * decodeURIComponent() Decodes all characters encoded by encodeURIComponent()

 */

  

let url = 'http:// www.azoux.com/azou domy.js'

console.log(encodeURI(url)); // http://%20www.azoux.com/azou%20domy.js

console.log(encodeURIComponent(url)); // http%3A%2F%2F%20www.azoux.com%2Fazou%20domy.js

  
  

/**

 * eval()Method: this method is a complete ECAMScript interpreter, which accepts a parameter, that is, a JavaScript string to be executed

 * When the interpreter finds Eval, it inserts the code in the eval into that location

 * The code called through eval belongs to the execution context of the call

 */

  

eval('console.log("azoux");');

// Equivalent to console log("azoux");

  

/**

 * Global Object properties

 * There is a table, basically some constructors and special values

 */

  

/**

 * window object

 * The browser implements the window object as a proxy for the Global object

 * Another way to get Global objects

 */

  

let global = function () {

 return this;

}();

  

console.log(global); // window

Math

/**

 * Math Object attribute: specific table lookup

 */

  

/**

 * min()And max() methods

 * Used to determine the maximum and minimum values of a set of values

 */

console.log(Math.min(...[1, 2, 3, 4, 5, 6])); // 1

console.log(Math.max(...[1, 2, 3, 4, 5, 6])); // 6

  

/**

 * Rounding method

 * Math.ceil():  Always round up to the nearest integer

 * Math.floor(): Always round down to the nearest integer

 * Math.round(): rounding

 * Math.fround(): Returns the single precision (32-bit) representation of the closest value

 */

  

/**

 * random()Method: randomly return decimals between 0 and 1

 */

  

/**

 * Other methods: look up the table

 */