Relearn JavaScript 1 (continuously updated)

1. Data type

Primitive value type "value type/basic data type"
number Number: NaN (not a valid number) Infinity (infinity value)
string string
boolean Boolean
null null object pointer
undefined undefined
symbol unique value
bigint large number
object type "reference data type "

  • Standard ordinary object object
  • Standard special objects Array, RegExp, Date, Math, Error, ArrayBuffer, DataView, Set Map...
  • Non-standard special objects Number, String, Boolean, Symbol, BigInt... The format information of the original value object type created based on the constructor or Object, the type belongs to the object type (Symbol and BigInt cannot be created through new)
  • Callable/executable object "function" function (implements the call method)
	if(NaN === NaN){
    
    
	//不能基于"是否等于NaN"来检测值是否为有效数字
	}
	isNaN([value])//:不论[value]啥类型,默认隐式转换为数字类型 Number([value]), 再校验是否为有效数字,如果是有效数字,返回false
	Object.is(NaN,NaN)//:true [不兼容IE(Edge除外)]

Symbol small example
Insert image description here
bigint
Insert image description here

2. Data type detection (typeof)

The following are common data type detection methods. Currently, only typeof will be explained. The remaining ones will be written later.
Insert image description here

typeof detects data type

typeof detection type

  • typeof[value]: Returns the string of the type to which value belongs, for example: "number" / "string"
  • Cannot detect null and return object
  • Except for callable objects, which 函数return “function"arrow functions, constructors, generator functions, and ordinary functions, all return function types. All other object data values ​​return Object.
  • Detecting an undeclared variable will not report an error and return undefined
  • Typeof is detected using GetValue(val), which is a method provided internally by C++ and is detected according to the stored binary.

typeof detects in binary

  • Object: Objects all start with 000, and null is 0000, so it will be detected as Object
    function. Because call is implemented internally, function will be returned.
  • null:0000
  • undefined: -2^30
  • Number: Integer 1 Floating point number 010
  • String: 100
  • Boolean: 110
    typeof detects data types very quickly, and detects primitive value types (except null) very accurately.

data structure

Insert image description here

array structure

Insert image description here

Stack structure (first in, last out), stack pushing and popping are operated on the same end

class Stack {
    
    
    // => this.container=[];
    container = [];
    enter(item) {
    
    // 进栈
        this.container.push(item);
    }
    leave() {
    
     // 出栈
        return this.container.pop();
    }
    size() {
    
     // 长度
        return this.container.length;
    }
    value() {
    
    // 内容
        return this.container.slice(0);
    }
}
let sk1 = new Stack;
sk1.enter(10);
sk1.enter(20);
sk1.enter(30);
console.log(sk1.leave())

The maximum base and minimum base are 36 and 2 respectively
Browsers generally consider numbers written in js to be decimal.
If it starts with 0x, the browser will recognize that this is a hexadecimal value, and then change it into decimal. The result is 0x100=>256
. The number starts with 0, and the browser will recognize that this is an octal value, and then change it into a decimal value, 012 => 10

Quick exercise: Convert decimal to binary

  1. 10.toString(2) //toString is a method on the Number prototype
  2. Implement your own method of adding a number to the prototype of Number
Number.prototype.decimal2binary = function decimal2binary() {
    
    
    // this->new Number(28)  decimal->28
    let decimal = +this;
    if (/\./.test(decimal)) {
    
    
        // 包含小数
        throw new RangeError('处理的数字必须是整数');
    }
    // 整数
    let sk = new Stack;
    if (decimal === 0) return '0';
    while (decimal > 0) {
    
    
        sk.enter(decimal % 2);
        decimal = Math.floor(decimal / 2);
    }
    return sk.value().reverse().join('');
};

let num = 28;
console.log(num.toString(2));
console.log(num.decimal2binary());

Queue structure: first in, first out, last in, last out "Enter from one end, but need to exit from the other end" => EventLoop involves priority queue

class Queue {
    
    
    container = [];
    // 从顶端进入队列
    enter(item) {
    
    
        this.container.unshift(item);
    }
    // 从底端出队列
    leave() {
    
    
        return this.container.pop();
    }
    // 长度
    size() {
    
    
        return this.container.length;
    }
    // 内容
    value() {
    
    
        return this.container.slice(0);
    }
}

Small exercise: Drumming and passing flowers
. N people play the game together. They form a circle and count from 1. Those who count to M will be automatically eliminated. The last remaining person will win. Who is the original person left at the end?

//   + n参与的人数 >=1
//   + m被淘汰的数字
const game = function game(n, m) {
    
    
    // 让参与的人依次进入队列
    let qe = new Queue;
    for (let i = 1; i <= n; i++) {
    
    
        qe.enter(i);
    }

    // 只要队列中还存在两个及两个以上的人,则重复这个逻辑
    while (qe.size() > 1) {
    
    
        for (let i = 1; i < m; i++) {
    
    
            // 前m-1个人,出队列后立即进入队列
            qe.enter(qe.leave());
        }
        // 数到m的人直接出队列被淘汰了
        qe.leave();
    }

    return qe.value()[0];
};
console.log(game(8, 5));

Data type conversion

Number

Convert the object to a number:
+ First call the object's Symbol.toPrimitive method. If this method does not exist
+ Then call the object's valueOf to get the original value. If the obtained value is not the original value
+ Then call the object's toString to turn it into a character. String
+ Finally, convert the string into a number based on the Number method.
Insert image description here
Small exercise

let arr = [27.2, 0, '0013', '14px', 123];
arr = arr.map(parseInt);//[NaN,NaN,1,1,27]

String

Convert object obj to string

  • String(obj): Symbol.toPrimitive -> valueOf -> toString The default implicit conversion of the browser is String(obj)
  • obj.toString(): Call this method directly to convert a string, and the above rules will not be implemented.
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44157964/article/details/119044443