This is the first day of my participation in the August Challenge. For details, see:August is more challenging

Front-end eight-part review, continuously updated. Autumn recruit self… Cut the crap and get right to it…

The body of the

Js

  • What are the original types of JS? There are seven

String, Nndefined, Number, Boolean, Symbol, Null, and BigInt

  • Is the following legal?console.log('1'.toString())

It’s legal. Although ‘1’ is the original type, there is an implicit conversion and ‘1’ is automatically read as a string object

  • What are the differences between object types and primitive types?

Primitive types store values on the stack. Object types store addresses (Pointers) in the stack and values in the heap.

  • What happens when function arguments are objects?

If the function is passed as an object, it is passed as the reference address of the object. If the object is modified inside the function, the object will also be changed outside the function.

function test(person){
    person.age = 20
    person = {
        name:'total yuan'.age:30
    }
    return person
}
const p1 = {
    name:'胑总'.age:25
}
const p2 = test(p1) // The function passes the reference address of the object
console.log(p1) //{Mr. Hu, 20}
console.log(p2) //{yuan Zong, 30}
Copy the code

Js type determination

  • Does Typeof correctly determine the data type?

No, typeof can only correctly determine primitive types and functions other than null

  • typeof null //object ?

In the original version of JS, the 32-bit system was used. For performance purposes, the type information of variables was stored in low order. The beginning of 000 represents an object, whereas null represents all zeros, so it was wrongly identified as object. Although the current internal type determination code has changed, this Bug has persisted.

  • What’s the difference between typeof and instanceof?
  1. Typeof can only correctly determine primitive types and functions other than null
  2. Instanceof can accurately identify objects with prototypes
  • How does instanceof perform type determination?

Implicit stereotype of instance object = explicit stereotype of constructor

  • Instanceof implementation principle

– > handwritten instanceof

 function myInstanceof(obj, type) {
    let a = obj.__proto__,
    b = type.prototype

    while(a ! = =null) { // This is where the first layer is not found. Go up the prototype chain
        if (a === b) return true
        a = a.__proto__
    }
    return false
}
Copy the code
  • Js type judgment of all methods – from the use, the implementation of the type judgment principle, performance analysis of the difference

Five kinds of methods: typeof, instanceof, Object. The prototype. ToString, call (), constructor, Array. IsArray

  1. typeof
  • In use, Typeof can determine primitive types and functions other than null
  1. instanceof
  • Instanceof object === true
  • Prototype = type.prototype According to this essence, the object travels up the stereotype chain until it finds an explicit stereotype of the type
  1. Object.prototype.toString.call()
  • In use, Object prototype method, other objects are called by call/apply -> [Object XXX]
  • Can be used to determine all data types
  1. constructor
  • New Number(1). Constructor === Number. Constructor === Number
  • Invalid for undefine and NULL
  • In addition to the string, array, window, document can be directly judged. For other data types, the method must be a new instance object in order to determine the type

5. Array.isArray()

  • Determines whether an object is an array
  • If this method is not available, it can be implemented through method 3

Differences and pros and cons:

  1. Differences in use
    • Typeof can determine primitive types and functions other than null
    • Object. The prototype. ToString. Call () can be used to determine all the data types, used to determine the browser built-in objects
    • Instanceof can only be used to determine reference types, not primitive types, and all reference types instanceof Object are true
    • Constructor does not work with undefined or null
    • Array.isarray () can only be used to determine whether an object is an Array
  2. Differences in implementation principles
  3. When determining an Array object, array. isArray is better than instanceof because it can determine an Array of iframe objects
  4. Constructor is unstable, which is mainly reflected in the custom Object. If the prototype of the Object is overwritten, the original constructor reference will be lost and constructor will default to Object
  5. In performance, 3 is as bad as 5, instanceof is better, construcor is best (not necessarily)

Js type conversion

  • There are only three types of cast in js
    • Convert to a Boolean value
    • Converted to digital
    • Convert to string

  • [[ToPrimitive]] eg: {} -> true

    1. If it is already a primitive type, no conversion is required
    2. If not, x.valueof () is called, and if converted to the underlying type, the converted value is returned
    3. If 2 has not been successfully converted to the base type, x.tostring () is called, and if converted to the base type, the converted value is returned
    4. If neither is converted to the original type, an error will be reported
  • arithmetic

  1. In the process of operation, as long as one side is a string, all sides will be converted to a string, and then string concatenation example: 1 + ‘1’ // ’11’
  2. As long as either of them is not a string or a number, they are first converted to a number or a string: true + true //2
  3. In addition to the addition operation, as long as one of the four operations is a number, the other will be converted to a number
4 + [1.2.3]
1. [1.2.3].valueOf()
2. [1.2.3].toString() / / "1, 2, 3"
3. 4 + "1, 2, 3"
4. "41, 2, 3"


'a' + + 'b'
1. 'a' + (+ 'b')
2. 'a' + NaN
3. 'a' + 'NaN'
4. 'aNaN'

Copy the code

This points to the problem

  1. Non-arrow functions: my this refers to the person who calls me; In the case of a stand-alone call,this points to window
  2. Arrow function: this refers to the lexical scope of the function (where the function is defined)

conclusion

The front end of the eight-part article naturally more than these, this article is only part, but each point is more detailed, will continue to update!