4. Data types

5, Number

1. Floating point

2. Value range

3, NaN

  • Principle:
    • ValueOf () determines whether the returned value can be converted to a numeric value
    • 2. If not, call toString() and test the return value

4. Numerical conversion

  • Number()
    • boolean true -> 1 false -> 0
    • Number returns
    • null -> 0
    • undefined -> NaN
    • string
      • Contains numeric values, converted to decimal
      • Contains floating point, convert to floating point
      • Contains hexadecimal characters such as 0xf converted to decimal
      • An empty string that returns 0
      • Other NaN
    • object
      • ValueOf () converts the return value according to the above rules
      • If the result is NaN, call toString(), converting by string
  • parseInt()
    • Priority is given to getting integers
    • The leading space is ignored
    • The first one, which is not a numeric addition or subtraction, returns NaN, and the null character also returns NaN
    • The first is a number plus or minus, and the first non-number is immediately ignored
    • Identify the decimal notation that starts with 0x and 0
    • The second argument, as a base number parseInt(‘AF’, 16) -> 175
  • parseFloat()
    • The first decimal point is valid, the second decimal point is invalid
    • Always ignore the starting 0 (base 10) and parse only base 10

6, the String

1. String literals

  • Length Specifies the length (double bytes are not allowed).

2, the characteristics of

  • Immutable are recreations

3. Convert to a string

  • toString()
    • Returns the string equivalent of the current value
    • Applies to values, booleans, objects, and strings. Null and undefined are not supported
    • The value of the pass parameter represents the base
  • String()
    • If there is a toString() method, that method is called to return the result
    • null-> “null”
    • undefined -> “undefined”

Template literals

  • I can just wrap it
  • Save space

5. String interpolation

  • The ${}
  • Strong string by toString()

Template literals tag functions

7. Raw string

  • String.raw

7, Symbol

1. Basic Usage

  • Initialize the Symbol ()
  • Return to symbol typeof
  • You can pass string arguments as descriptions, and you can debug, but not define identity
  • You can’t create it with new, because you really need to be able to use Object() to avoid creating symbols to wrap objects.

2. Use the global symbol registry

  • Share and reuse Symbol instances, using a string as the key symbol.for ()

    let foo = Symbol.for('foo')
    let other = Symbol.for('foo')
    console.log(foo == other) // true
    Copy the code
  • Even if the same symbol is used, it is not equivalent

    let local = Symbol('foo')
    let other = Symbol.for('foo')
    console.log(local == other) // false
    Copy the code
  • Must be a string

  • Symbol.keyfor () queries the global registry and returns the corresponding string key

3. Use symbols as attributes

  • Symbols are used wherever you can use strings or values as attributes.

  • To get the key

    • Object. GetOwnPropertySymbols () to obtain symbol key
    • Object. GetOwnPropertyNames () to obtain the conventional key
    • Reflect.ownkeys () returns all keys
  • If you do not explicitly save it, you must traverse to find it.

    let o = {
      [Symbol('foo')]: 'foo val'[Symbol('bar')]: 'bar val'
    }
    let barSymbol = Object.getOwnPropertySymbols(o).find((symbol) = >symbol.toString().match(/bar/))
    Copy the code

4, commonly used built-in symbols

  • A for-of loop uses the symbol. iterator property on the related object
  • You can rewrite to change the for-of behavior

5, Symbol. AsyncIterator

  • As a property represents “a method that returns the object’s default AsyncIerator.” Used by for-await-of statement”
  • Asynchronous iterator for-await-of

6, Symbol. HasInstance

  • As a property represents “a method that determines whether a constructor object recognizes an object as its instance, used by the instanceof operator.”

    function Foo(){}
    let f = new Foo()
    f instansof Foo // true
    Foo[Symbol.hasInstance](f)
    Copy the code

7, Symbol. IsConcatSpreadable

  • An attribute means “a Boolean value. If true, it means its Array elements should be flattened with array.prototype.concat ().”

    let init = ['foo']
    let arr = ['bar']
    arr[Symbol.isConcatSpreadable] // undefined
    init.concat(arr) // ['foo', 'bar']
    arr[Symbol.isConcatSpreadable] = false
    init.concat(arr) // ['foo', Array[1]]
    
    let arrLikeObj = {length: 1.0: 'baz'}
    arrLikeObj[Symbol.isConcatSpreadable] // undefined
    init.concat(arrLikeObj) // ['foo',{...}]
    arrLikeObj[Symbol.isConcatSpreadable] = true
    init.concat(arrLikeObj) // ['foo', 'baz']
    Copy the code

8, Symbol. The iterator

  • A property means “a method that returns the default iterator for an object, used by for-of”

9, Symbol. The match

  • An attribute represents “a regular expression method that uses regular expressions to match strings, as used by the string.prototype.match () method.”
class StringMatcher{
  constructor(str) {
    this.str = str
	}
  [Symbol.match](target) {
    return target.includes(this.str)
  }
}
Copy the code

10 and Symbol. The replace

  • An attribute represents “a regular expression method that replaces matched substrings in a String, used by the string.prototype.replace () method.”

11, Symbol. The search

  • An attribute represents “a regular expression method that returns the index of a String matching a regular expression, used by the string.prototype.search () method.”

12, Symbol. Species

  • A property represents “the value of a function that serves as a constructor for creating a derived object,” and a getter can override the stereotype definition of a newly created instance

13 and Symbol. The split

  • A property represents “a regular expression method that splits a String at the index position matching the regular expression, used by string.prototype.split”

14 and Symbol. ToPrimitive

  • An attribute represents “a method that converts an object to the corresponding raw value used by the ToPrimitive abstract operation.”

15 and Symbol. ToStringTag

  • An attribute said “a string, the string is used to create the default string Object description, Object by an internal method. The prototype. The toString () the use of”

16 and Symbol. Unscopables

8, the Object

  • Properties and Methods
    • Constructor creates the function of the current object
    • HasOwnProperty () determines whether the given property exists on the current object instance (not the stereotype).
    • IsPrototypeOf Determines whether the current object is a prototype of another object
    • PropertyIsEnumerable () specifies whether a given property is a for-in enumeration
    • ToLocalString () returns a string representation of the object that reflects the local execution environment
    • ToString () returns a string representation of the object
    • ValueOf () returns the string, numeric, or Boolean representation of the object.
  • Object is the accumulation of all objects, and any Object has these properties and methods.

5. Operators

  • These include mathematical operators, bitwise operators, relational operators, and equality operators
  • Can be used for a variety of values, including strings, booleans, and even objects.
  • When applied to objects. The valueOf() and/or toString() methods are usually called

1. Unary operator

  • Only one value is operated on

1. Increase/decrease

  • Prefixes work before suffixes
  • A string in which a valid value is converted to a value before being applied. string -> number
  • String, not a valid value, converted to NaN. string -> number
  • Boolean, false to 0, true to 1, and then applied. boolean-> number
  • Floating-point value, plus or minus 1
  • Object, which calls the valueOf() method to get an operable value.

2. Unary addition and subtraction

  • Value has no impact (+)
  • Non-numeric equivalent to using Number()

2. Bit operators

  • The binary encoded storage of a negative binary complement (complement)
  • NaN and Infinity are treated as 0 in in-place operations
  • Non-numeric values are converted with Number()

1, according to the bit non ~

  • Returns the complement of a numeric value

    let num1 = 25    / / 00000000000000000000000000011001
    let num2 = ~num1 / / 11111111111111111111111111100110
    num2 / / - 26
    Copy the code
  • The effect is the same as negative minus 1 or negative plus 1

2, according to the bit and &

Each bit is binary aligned, 1 if the same 1, 0 otherwise

3, by bit or |

Each bit is binary aligned, 1 if there is 1, 0 otherwise

4, by bit xor ^

Each bit of binary alignment is 0 if it is the same, 1 if it is different

5, left shift <<

Binary followed by 0 (left shift), to the power

2 << 5
10 -> 1000000 / / 64
Copy the code

6, symbols move right >>

  • Keeping positive and negative is equivalent to << inverse operation

7, unsigned right move >>>

  • A positive number is the same as a signed shift to the right

  • Negative numbers are treated as positive numbers and shifted to the right

    let old = -64
    let new = old >>> 5
    / / - 64, 11111111111111111111111111000000
    / / 00000111111111111111111111111110
    // new 134217726
    Copy the code

Boolean operators

1, logic non!

  • Always return a Boolean value
  • First convert operands to Booleans, then invert
  • The rules
    • Object to false
    • Empty string true
    • A non-empty string false
    • The number 0 true
    • The value is non-0 (Infinity) false
    • null true
    • undefined true
  • !!!!! Equivalent to Boolean ()

2. Logic &&

  • T&&T -> T Other F
  • Can be used on any type of operand, not limited to Boolean values, and does not necessarily return Boolean values
    • The first operand is the object and returns the second operand
    • The second operand is an object, which is returned only if the first operand evaluates to true
    • Both operands are objects and return the second operand
    • One of the operands is NULL and returns NULL
    • One of the operands is NaN and returns NaN
    • One of the operands is undefined and returns undefined
  • The short-circuit operator will never evaluate the second operand if the first operand determines the result
    • The first operand is false. The second operand is not evaluated

3, logical or | |

  • Other T F | | F – > F
  • Can be used on any type of operand, not limited to Boolean values, and does not necessarily return Boolean values
    • The first operand is the object that returns the first operand
    • The first operand evaluates to false and returns the second operand
    • Both operands are objects and return the first operand
    • Both operands are NULL, and null is returned
    • Both operands are NaN and return NaN
    • Both operands are undefined, return undefined
  • The short-circuit operator will never evaluate the second operand if the first operand determines the result
    • The first operand is true; the second operand is not evaluated

4. multiplicative operator

  • An empty string acts as a 0 and a Boolean true acts as a 1

The multiplication operator *

  • If they are all numbers, perform the normal operation.
  • Any NaN, returns NaN
  • Infinity * 0 -> NaN
  • Infinity * a nonzero finite number, returned by symbol
  • Infinity*Infinity -> Infinity
  • Instead of a numeric value, the Number() conversion is followed by the calculation.

2. The division operator /

  • If they are all numbers, perform the normal operation.
  • Any NaN, returns NaN
  • Infinity/Infinity returns NaN
  • 0/0 returns NaN
  • A nonzero finite number divided by 0 returns Infinity or -infinity depending on the first operand sign
  • Infinity divided by any number, returning Infinity or -infinity depending on the second operand sign
  • Instead of a numeric value, the Number() conversion is followed by the calculation.

3. Fetch operator %

  • If they are all numbers, perform the normal operation.
  • If the dividend is infinite and the divisor is finite, return NaN
  • If the dividend is finite and the divisor is 0, return NaN
  • If Infinity % Infinity returns NaN
  • Returns the dividend if the dividend is finite and the divisor is infinite
  • If the dividend is 0 and the divisor is not 0, return 0
  • Instead of a numeric value, the Number() conversion is followed by the calculation.

5, the index operator **

  • * * () Math. The pow equivalent

  • The index assignment operator **=

    let s = 3
    s **= 2
    s / / 9
    Copy the code

Additive operator

1. Addition +

  • Is the value
    • Any NaN, returns NaN
    • Infinity plus Infinity -> Infinity
    • -infinity plus -infinity -> -infinity
    • Infinity plus -infinity -> NaN
    • Plus 0 plus 0 minus > plus 0
    • Minus 0 plus plus 0 minus > plus 0
    • Minus 0 plus minus 0 minus > minus 0
  • A string
    • Both are strings, and the second one has to be concatenated after the first one
    • You just have one string, turn the other one into a string, concatenate it
    • The toString() method is called to get the string, which is then concatenated
    • For undefined null, call String(), return String undefined null

2. Subtraction

  • It’s all numbers. You do mathematical subtraction
  • Any NaN, returns NaN
  • Infinity minus Infinity -> NaN
  • -infinity minus -infinity -> NaN
  • Infinity minus – Infinity ->Infinity
  • -infinity minus Infinity -> -infinity
  • Plus 0 minus plus 0 minus > plus 0
  • Plus 0 minus minus 0 minus > minus 0
  • Minus 0 minus 0 minus > plus 0
  • Any operand is a string, Boolean, null, undefined. Call Number() in the background
  • Call valueOf() if any operand is an object, or toString() if none

Relational operators

  • It’s all numerical, numerical comparison
  • All are strings, which are compared one by one
  • Any number is converted to another number and then compared
    • “A” < 3, “a” is converted to the value NaN, and returns false
  • Any object, call valueOf(), get the result and compare, no valueOf(), use toString()
  • Any Boolean, convert to numeric comparison
  • NaN comparisons are all false

The equality operator

1. Equal and not equal

Type conversions are performed

  • Any Boolean, convert to a number and compare.
  • A character, a number, a character converted to a number and then compared
  • One object, not the other, valueOf() gets the original value and compares it
  • null == undefined
  • Null and undefined cannot be converted to other types of values for comparison
  • For any NaN, false is returned for equality and true for inequality
  • They are all objects, and they compare whether they are the same object.
null= =undefined 		// true
"NaN"= =NaN					// false
5= =NaN							// false
NaN= =NaN						// false
NaN! =NaN 						// true
false= =0						// true
true= =1							// true
true= =2							// false
undefined= =0 				// false
null= =0							// false
"5"= =5							// true
Copy the code

2. Congruence and incongruence

Not converting type

It is recommended to use

9. Conditional operators

The assignment operator

  • Simple assignment
  • The compound assignment

The comma operator

  • Perform multiple operations
  • Returns the last value

6, statements,

1, if

2. Test the loop after do-while

3, while first test the loop

4, the for

5, the for – in

  • Enumerates the nonsigned key properties of an object

6, the for –

  • An iteration statement that iterates over an iterated object element
  • For-await-of supports generating asynchronous iterables with terms

7. Label statements

8. Break and continue

  • Break immediately exits the loop, forcing the next statement after the loop
  • Continue exits the loop immediately, but executes again from the top of the loop
  • Can be executed with label statements, exit to execute loops, mostly used for loop nesting

9, with

  • Sets code scope to a specific object
  • Is not recommended

10 and the switch

  • Switch (true) logic is clearer, each expression is evaluated until it returns true
  • Congruent to compare

7, functions,

  • Either return a value or not return a value at all

  • Strict mode

    • Cannot use eval, arguments as names

    • Arguments cannot be called eval or arguments

    • Two named parameters cannot have the same name