As an ordinary JS beginner, from god three big man’s knowledge comb article benefit, write down my study notes.

JS data type

function test(person) {
    person.age = 26
    person = {
      name: 'hzj'.age: 18
    }
    return person
  }
  const p1 = {
    name: 'fyq'.age: 19
  }
  const p2 = test(p1)
  console.log(p1) // { name: 'fyq', age: 26 }
  console.log(p2) // { name: 'hzj', age: 18 }
Copy the code

The person argument in the test function is a copy of the heap memory address of p1. The call to Person. age = 26 changes the value of p1, but then person is overwritten to become the address of another memory space, and finally returns the address of the other memory space to P2.

Look at the first five that seem to be objects because they have methods, but they’re immutable, right?

Numbers, strings, and booleans are not objects, but they do have properties and methods. This is because when referencing their properties and methods, they are converted to objects by calling new Number(), New String(), and New Boolean() wrapped objects, which inherit the corresponding methods to handle the attribute reference and destroy the temporary object once the reference ends. Null and undefined do not wrap objects, and accessing their properties will report type errors.

    console.log(typeof 1);  //number
    console.log(typeof new Number(1)); //object
    1= = =new Number(1) //fasle
Copy the code

Here we wrap 1 as an object, and that’s the difference between a primitive type and a wrapped object.

    let x = 32;
    console.log(x.toString(2));  / / 100000
    Call the instance method to destroy the instance
Copy the code

Methods and properties can also be used for raw values, because JS treats the raw values as objects when executing methods and properties. Simple datatypes cannot add attributes, but all but NULL and undefined have access to attributes, so they have the characteristics of objects, but are still simple datatypes, not objects.

JS accuracy loss

console.log(0.1 + 0.2); / / 0.30000000000000004
Copy the code

JS does not divide integer type and floating point type. When two floating point numbers are added together and converted into binary, the loop will be infinite. Due to the limitation of standard bits, the redundant bits will be cut off, resulting in precision loss.

BigInt

In JS, all numbers are represented in double precision 64-bit floating-point format, which causes the Number in JS to be unable to accurately represent very large integers, which are rounded off, The Number type in JS can only safely represent -9007199254740991(-(2^53-1)) and 9007199254740991((2^53-1)). Any integer value outside this range may lose precision.

    const theBiggestInt = 9007199254740991n
    console.log(theBiggestInt, typeof theBiggestInt); // 9007199254740991n 'bigint'
    console.log(theBiggestInt + 1n);      // 9007199254740992n
    console.log(theBiggestInt * 10n);     // 90071992547409910n
Copy the code

JS type change

=== is called strict equality, which means that the left and right sides not only have the same values, but also have the same types. == is not as strict as ===, which generally returns true as long as the values are equal, but == also involves some type conversions.

Object to primitive type

// toPrimitive is called first and toString is called last
 var obj = {
     value: 3.valueOf() {
        return 4;
     },
     toString() {
       return '5'},Symbol.toPrimitive]() {
       return 6}}console.log(obj + 1); / / output 7
Copy the code

closure

  • Function internal nested functions contain objects that reference variables (functions).

The essence of closures is that there are references to the parent scope in the current environment.

var f3;
function f1() {
  var a = 2
  f3 = function() {
    console.log(a);
  }
}
f1();
f3(); / / 2
Copy the code

F1 () is called to assign value to F3 (). At this time f3() has access to the scopes of window, f1 and f3 itself. There is a reference to the parent scope of variable F3, so a closure is generated.

JS implementation inheritance

  function Parent () {
    this.name = 'parent';
    this.play = [1.2.3];
  }
  function Child() {
    Parent.call(this);
    this.type = 'child';
  }
  Child.prototype = Object.create(Parent.prototype);
  Child.prototype.constructor = Child;
Copy the code

Combination-oriented inheritance

Composition oriented is to design a series of parts and then assemble them to form different instances or classes. The code is clean and reusable. This is combination-oriented design.

function drive(){
  console.log("wuwuwu!");
}
function music(){
  console.log("lalala!")}function addOil(){
  console.log("Oh dear!)}let car = compose(drive, music, addOil);
let newEnergyCar = compose(drive, music);

Copy the code

Question of native JS soul, how many can you catch? (on)