This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

  • 📢 hello everyone, I am Xiao Cheng, a student, recently in the brush little Red book, this is a study notes
  • 📢 wish you and I together in this wanton life big shine
  • This is the fifth day of reading advanced Programming in JavaScript (4th edition). The book has been read 135/865

Chapter 5: Basic reference types

At the beginning of the sentence: “Functions are also reference types, but there are too many to fit into one chapter, so a later chapter!!” Outrageous!

Complaining words

  • After writing it for a while, I realized that this chapter is a lot to remember, which is very difficult
  • I hate CV all the time!

5.1 the Date

To create a Date object, call the Date constructor using the new operator

let now = new Date(a);Copy the code

Date.now() returns the number of milliseconds at the current time

The date.parse () method takes a string argument representing a Date and attempts to convert the string to the number of milliseconds representing the Date. If the value passed does not represent a time, then NaN is returned

let someDate = new Date(Date.parse("May 23, 2019"));
Copy the code

There’s also date.utc (), which I don’t really understand, it’s not very common

5.1.1 Methods of inheritance

The Date type overrides these methods

The valueOf method returns the millisecond valueOf the date

5.1.2 Date Formatting Methods

The Date type has several methods dedicated to formatting dates, all of which return a string:

  • ToDateString () displays the day, month, day, and year of the week in the date (in an implementation-specific format);

  • ToTimeString () displays the hour, minute, second, and time zone in the date (in an implementation-specific format);

  • ToLocaleDateString () displays the day, month, day, and year of the week in the date (in an implementation – and locale-specific format);

  • ToLocaleTimeString () displays the time, minute, and second in the date (in an implementation – and location-specific format);

  • ToUTCString () displays the complete UTC date (in an implementation-specific format).

5.1.3 Date/Time component methods

Step up to CV engineer (forgive)

Commonly used as noted in the previous notes, commonly used built-in objects

5.2 the RegExp

The old cliche, regular expressions, tired

For regular expression syntax, see

5.2.1 RegExp Instance Properties

5.2.2 RegExp Instance method

The exec() method, which takes only one argument, the rule to match the string. If a match is found, an array containing the first match is returned; If no match is found, null is returned

The test() method, which takes a string argument. The argument returns true if the input text matches the pattern, false otherwise

let text = "000-00-0000.";
let pattern = /\d{3}-\d{2}-\d{4}/;
if (pattern.test(text)) {
	  console.log("The pattern was matched.");
}
Copy the code

Similar to the

5.2.3 RegExp constructor property

Such as:

let text = "this has been a short summer"; 
let pattern = / (.). hort/g; 
if (pattern.test(text)) { 
 console.log(RegExp.input); // this has been a short summer 
 console.log(RegExp.leftContext); // this has been a 
 console.log(RegExp.rightContext); // summer 
 console.log(RegExp.lastMatch); // short 
 console.log(RegExp.lastParen); // s 
}
Copy the code

5.3 Raw value packaging type

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 methods used to manipulate the primitive value.

For the examples in the book:

let s1 = "some text";/ / the original value
let s2 = s1.substring(2);
console.log(s2);//'me text' is running properly
Copy the code

You go through the following steps

  1. Create an instance of type String

  2. Call a special method on the instance

  3. Destroy instance

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

The life cycle of the raw value wrapper type exists only on the line of code accessed, so you cannot add attributes and methods to the raw value

5.3.1 Boolean

The difference between raw values and Boolean objects (reference values) :

  1. The Typeof operator returns Boolean for raw values and object for reference values

  2. Boolean objects are instances of type Boolean that return true when using the instanceof operator or false for raw values

The following is an example:

let falseObject = new Boolean(false);
let falseVlaue = false;
console.log(typeof falseObject)//object
console.log(typeof falseVlaue)// Boolean
console.log(falseObject instanceof Boolean)// true
console.log(falseVlaue instanceof Boolean)// false
Copy the code

5.3.2 Number

To create a Number object, use the Number constructor, as shown in the following example:

let numberObject = new Number(2)
Copy the code

The Number type overrides the valueOf(), toLocaleString(), and toString() methods

The toString() method optionally takes an argument representing cardinality and returns a numeric string in cardinality form

let num = 10;
console.log(num.toString());/ / '10'
console.log(num.toString(2));/ / '1010'
console.log(num.toString(8));/ / '12'
console.log(num.toString(10));/ / '10'
console.log(num.toString(16));//'a'
Copy the code
1. ToFixed () method

Returns a numeric string containing the specified number of decimal points, as shown in the following example:

let num = 10;
console.log(num.toFixed(2));/ / '10.00'
Copy the code

The number of decimal places from 0 to 20

2. ToExponential () method

Returns a numeric string in scientific notation

let num = 9;
console.log(num.toExponential(1));/ / '9.0 e+0'
console.log(num.toExponential(2));/ / '9.00 e+0'
Copy the code
3. The toPrecision () method

In contrast to the previous method, this method is more reasonable, and also scientific enumeration

let num = 99; 
console.log(num.toPrecision(1)); // "1e+2" 
console.log(num.toPrecision(2)); / / "99"
console.log(num.toPrecision(3)); / / "99.0"
Copy the code
4. IsInteger method and secure integer

The number.isinteger () method to determine whether a value is saved as an integer.

console.log(Number.isInteger(1));// 'true'
console.log(Number.isInteger(1.00));// 'true'
console.log(Number.isInteger(1.01));// 'false'
Copy the code

5.3.3 String

Create a String

let stringObject = new String('javaScript');
Copy the code
1. The JavaScript characters

JavaScript strings consist of 16-bit Code units. The length property of the string indicates how many code units the string contains.

Very advanced, I don’t deserve hahaha

let msg = 'javaScript';
console.log(msg.length)/ / 10
Copy the code
2. CharAt () method:

Returns the character at the given index position (easy to understand)

let msg = 'javaScript';
console.log(msg.charAt(2))// 'v'
Copy the code
3. CharCodeAt () method:

Returns the value of code Unit at the specified index position, which is specified as an integer

let msg = 'javaScript';
console.log(msg.charCodeAt(2))/ / '118'
Copy the code
4. FormCharCode () method:

The character used to create a string based on a given UTF-16 code unit. It can take any number of values and returns a string that concatenates the characters corresponding to all values

console.log(String.fromCharCode(97.98.55357.56842.100.101)); / / ab ☺ DE
Copy the code

Good for you

5. String operations
1. concat

Used to concatenate one or more strings into a new string

let stringValue = 'javaScript ';
let res = stringValue.concat('nice')
console.log(res)//'javaScript nice'
Copy the code
Slice, substr, and substring

Three methods to extract the string

  1. Slice (start substring position, end substring position)

  2. Substring (start, end of substring)

  3. Substr (start of substring, number of substrings returned)

Normal operation

let strVal = 'javaScritp nice';
console.log(strVal.slice(3));//'aScritp nice'
console.log(strVal.substr(3));//'aScritp nice'
console.log(strVal.substring(3));//'aScritp nice'
 
console.log(strVal.slice(2.6));//'vaSc'
console.log(strVal.substring(2.6));//'vaSc'
console.log(strVal.substr(2.6));//'vaScri' the second argument indicates the number of characters to return
Copy the code

Unconventional operation

  1. Slice () : Treat all negative arguments as string length plus negative argument value

  2. Substr () : Add the first negative argument value as the length of the string, and convert the second negative argument value to 0

  3. Substring () : Converts all negative parameter values to 0

let strVal = 'javaScritp nice';
console.log(strVal.slice(-3));//'ice'
console.log(strVal.substr(-3));//'ice'
console.log(strVal.substring(-3));//'javaScritp nice'
 
console.log(strVal.slice(2, -3));//'vaScritp n'
console.log(strVal.substring(2, -3));//'ja'
console.log(strVal.substr(2, -3));/ /"
Copy the code
3. String position methods indeOf, lastIndexOf

IndexOf() and lastIndexOf() are used to locate substrings within a string;

IndexOf () looks for substrings from the beginning of the string;

LastIndexOf () looks for substrings from the end of the string;

let strVal = 'javaScritp nice';
console.log(strVal.indexOf('i'));/ / 7
console.log(strVal.lastIndexOf('i'));/ / 12
Copy the code
4. String contains methods

StartsWith (), endsWith(), and includes() are methods to determine whether another string is included in a string. These methods search through the string for the passed string and return a Boolean value indicating whether it is included

StartsWith () checks for a match that starts at index 0, with the second argument indicating where the search starts

EndsWith () checks for matches that start at the index string.length – substring.length()

Includes () checks the entire string

let strVal = 'javascript';
console.log(strVal.startsWith('java'))//true
console.log(strVal.startsWith('java'.1))//false
 
console.log(strVal.includes('java'))//true
console.log(strVal.includes('java'.2))//false
Copy the code
5. The trim () method

I’m familiar with this, delete before and after the whitespace, this method is very common in some data processing operations

let strVal = ' javascript ';
console.log(strVal.trim())//'javascript'
console.log(strVal)//' javascript '
Copy the code
6. Repeat () method

Receives an integer indicating how many times to copy and returns the concatenated result

let strVal = 'js ';
console.log(strVal.repeat(5) + 'nice')//'js js js js js nice'
Copy the code

Let’s look at it in ES6 after the iterative deconstruction

7. Case conversion

The methods involved in string case conversion are: toLowerCase(), toLocaleLowerCase(), toUpperCase(), toLocaleUpperCase()

ToLocaleLowerCase () and toLocaleUpperCase() are intended to be locale based;

let strVal = 'abcde javascaript';
console.log(strVal.toLocaleUpperCase())//'ABCDE JAVASCARIPT'
console.log(strVal.toUpperCase())//'ABCDE JAVASCARIPT'
console.log(strVal.toLocaleLowerCase())//'abcde javascaript'
console.log(strVal.toLowerCase())//'abcde javascaript'
Copy the code
8. LocaleCompare () method

Compare two strings, returning one of the following three values:

Returns a negative value (usually -1, depending on the implementation associated with the actual value) if the string should precede the string arguments in alphabetical order

Return 0 if the string is equal to the string argument;

Returns a positive value (usually 1, depending on the implementation associated with the actual value) if the string should be alphabetically followed by the string argument

let strText = 'blue';
console.log(strText.localeCompare('abc'))/ / 1
console.log(strText.localeCompare('blue'))/ / 0
console.log(strText.localeCompare('gjk'))// -1
Copy the code

5.4 Singleton built-in objects

5.4.1 Global

A Global object is a bottom line object. Code does not access it explicitly. It targets properties and methods that do not belong to any object

1. URL encoding method

EncodeURI () : used to encode the URI, not coding belongs to the special characters of URL components; EncodeURIComponent () : encodeURIComponent() is used to encode a single component in a URI, encoding any non-standard characters it finds, such as colons, slashes, greetings, and so on

let uri = "http://www.wrox.com/illegal value.js#start"; 
// "http://www.wrox.com/illegal%20value.js#start" 
console.log(encodeURI(uri)); 
// "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start" 
console.log(encodeURIComponent(uri));
Copy the code

DecodeURI () : decodes only characters encoded using encodeURI()

DecodeURIComponent () : decodes all characters encoded by encodeURIComponent();

2. The eval method

Stop!

5.4.2 Math

The Math object serves as a place to store mathematical formulas, information, and calculations, and provides properties and methods to aid in the calculations

1. The min and Max () ()

The operation is simple

 let max = Math.max(3.55.60.8.99);
 console.log(max);/ / 99
 
 let min = Math.min(3.55.60.8.99);
 console.log(min);/ / 3
Copy the code

For arrays, you can use the extension operator

let values = [1.2.33.9 ,8.7.5];
let max = Math.max(... values);console.log(max)/ / 33
Copy the code
2. Rounding method
  1. Math.ceil() : Always round up to the nearest integer;

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

  3. Math.round() : Rounding is performed;

  4. Math.fround() : Returns the closest representation of a numeric value to a single-precision (32-bit) floating point value;

3. The random () method

Pick a random number from 1 to 10

let number = Math.floor(Math.random() * 10 + 1);
console.log(number)
Copy the code

If it is need to generate a random number for encryption, then recommend the use of the window. The crypto. GerRandomValues ()