JavaScript

  • Seven built-in data type is null, undefined, number, Boolean, string, object, symbol
  • Object type function,array,date, etc
Use typeof to determine the data type
console.log(typeof 5);//number
console.log(typeof 'lucus');//string
console.log(typeof undefined);//undefined
console.log(typeof true);//boolean
console.log(typeof null);//object
Copy the code
  • Use typedefs to determine other basic data types, such as function,symbol, and null
Handwritten instanceof
/ / handwritten instanceof
const instancdofMock = (L, R) = > {
    if (typeofL ! = ='object') {
        return false
    }
    while (true) {
        if (L == null) {
            return false
        }
        if (R.prototype === L.__proto__) {
            return true
        }
        L = L.__proto__
    }
}
console.log(instancdofMock(' '.String))//false

function Person(name) {
    this.name = name
}
const p = new Person('lucus')
console.log(instancdofMock(p, Person))//true
Copy the code
Using the constructor and the Object. The prototype. ToString judgment data types (universal method, the ultimate method)
console.log(Object.prototype.toString.call('lucas'))
    //[object String]
console.log(Object.prototype.toString.call(undefined))
    //[object Undefined]
console.log(Object.prototype.toString.call(true))
    //[object Boolean]
console.log(Object.prototype.toString.call({}))
    //[object Object]
console.log(Object.prototype.toString.call([]))
    //[object Array]
console.log(Object.prototype.toString.call(function() {}))
    //[object Function]
console.log(Object.prototype.toString.call(null))
    //[object Function]
console.log(Object.prototype.toString.call(Symbol('lucas')))
    // [object Function]
Copy the code
  • Constructor returns an error if an attempt is made to read its constructor property, but nothing else
var foo = true
console.log(foo.constructor)
//Function: Boolean]
Copy the code

Type conversion

  • When the + operator is used to add string to other data types, all the other data types are converted to string, and in all other cases, to number, but undefined is converted to NaN
console.log(1 + '1')
/ / 11
console.log(1 + false)
/ / 1
console.log(1 + true)
/ / 2
console.log(1 + undefined)
//NaN
console.log('lucas' + true)
//lucastrue
Copy the code
  • When an object type is converted, valueOf is called first and then toString is called
  • Rewrite valueOf and toString
const foo = {
    toString(){
        return 'lucus'
    },
    valueOf(){
        return 1}}Copy the code
  • Concatenate if both sides of + are strings
  • If only one side is a string, the other values are converted to strings
  • If one side is an object, the valueOf or toString methods are called to get the value, which is converted to the primitive data type and then concatenated
Function transfer parameter
  • When a function parameter is a basic data type, a copy of the parameter value is copied inside the function. No operation affects the actual value of the original parameter
  • When a function parameter is a reference type, when an attribute of the value is modified in the function body, the original parameter is modified
  • When a function parameter is a reference type, if you change the reference address of the value directly, you create a reference in the function body, and nothing affects the actual value of the original parameter
const obj = {
    user: {
        posts: [{title: 'Foo'.comments: [ 'Good one! '.'Interesting... '] {},title: 'Bar'.comments: [ 'Ok'] {},title: 'Baz'.comments: []}],comments: []}}Copy the code
  • Using a try… Cathch method
var result
tyr {
    result = obk.user.posts[0].comments
}catch {
    result = null
}
Copy the code