js interview test summary

type of data

What are reference types, value types?

Value type key and value are stored on the stack (small amount)

The reference address of the reference type stored on the stack, and the data stored in the heap (large amount)

Assigning a reference type to a variable is to point the reference address of the variable to the address in the reference type heap

Which are value types?

String character, Number number, Boolean Boolean, undefined undefined, null empty (special), Symbol symbol

Which are reference types?

 Object object, Array number, Function function, Map diagram, Set collection

Judgment data type

typeof type
instanceof instance
constructor constructor
Array.isArray) is an array
Object.prototype.toString.callobj) prototype 

typeof judgment

Suitable for judging value types and reference types, but not specific reference types
typeof "abc” string
typeof 123 number
typeof true boolean
typeof undefined undefined
typeof null object
typeof Symbol()
symboltypeof object
typeof 0]object
typeof function()function
typeof new Map() object
type of new Set() object

instanceof

 Determine whether it is an instance on its prototype chain as long as the constructor returns true in the prototype chain
(created by Array, Array is a subclass of Object, instanceofArray and Object both return true)

[]instanceof Array true
[]instanceof Object true
{}instanceof Array false
{}instanceof Object true

constructor

Judging instance object constructor
[].constructor === Array true

most accurate

Object.prototype.toString.call(obj).slice(8,-1) returns the exact type of data

shallow copy

extends {...obj}
for iterating over
Object.assgin()

// 浅拷贝,只拷贝值类型,引用类型数据还是指向地址
var  obj1 = {name:"mumu",age:18,friend:["小红","小绿",{name:"小蓝",job:"teacher"}]};
// 01 ES6扩展,02 for循环 03 Object.assgin
var  obj2 = {...obj1}; //浅拷贝(如果属性值是引用类型,两个变量的属性都指向同一内存地址)
// 02 循环
var obj3 = {};
for(var k in obj1){
	obj3[k] = obj1[k];
}
// 03 Object.assgin 把参数的两个对象进行合并,属性相同后面覆盖前面
var obj4 = Object.assign(obj1,{});

deep copy

  1. JSON.parse(JSON.stringify(data))
    The json data class only supports Boolean, string, numeric null, undefined array object (other types of data such as functions will be ignored)
  2. Recursive deep copy by judging the type
    Recursion is the function calling itself (must have an end condition)

implicit conversion

The + string concatenation will attempt to convert other types to strings

+ - * / == will try to convert other types to numbers

  • Conversion failed NaN
  • false converts to 0; true converts to 1

 ><>= <= ! != == Judgment and logical return will try to convert other types to boolean values ​​(falsely variables are converted to false; empty strings, null, NaN, undefined, 0, are converted to false)

strictly equal to

===

Judging whether the type and value are relative
should use === at any time (it can be a special case when judging whether it is null or undefined)
null === null true

==

Judge the implicitly converted value
"100" == 100 // true
null == undefined //true
0 == false //true

Special case: NaN === null // false

{} == {} // false
[] == {} //false
points to a different memory address 

if judgment

Whether the judgment in if() is a truely variable

Falsely variable: false empty string 0 NaN undefined null (Twice negated !!a to get the result is false)

In addition to the falsely variable, all others are truely variables

logical and logical or

1. A||B

A is true (truly) the result is A, otherwise the result is B

2. A&&B

A is false (falsely) the result is A, otherwise the result is B

3. Judgment object

if(a&&a.b&&a.bc){}
if(a?.b?.c){}
If there is a and there is ab and there is abc
if(abc){} This is wrong 

Prototypes and Prototype Chains

noun

1. Class: (the template for creating an object instance, which is essentially a function)
        the constructor uses new to execute the function
        class xxx { }
2. Instance: the object created by the class (essentially speaking, it is an object)
3. Display the prototype
        class / Constructors have an explicit prototype prototype (essentially an object)
4. Implicit prototype
        Each instance has a private prototype __proto__
5. The explicit and implicit relationship
        class shows that the prototype prototype is equal to the implicit prototype of the instance it creates __proto__
6. When looking for the methods and properties of an object instance in the prototype chain
        , first look for it in itself, and if you can’t find it, look up along __proto__. The chain relationship formed by __proto__ is called the prototype chain (implements js inheritance)

The role of prototypes and prototype chains

Realize the inheritance of js
Realize the public properties and methods of the instance

explicit and implicit relationship

var arr = [];
arr.__proto__ === The Array.prototype
class reveals that the prototype prototype is equal to the implicit prototype __proto__ of the instances it creates 

prototype chain

js implements inheritance

1. The extends method of
class class Student extends People{ constructor(name,age,no){ // Inherit the constructor super(name,age) } }





2. Use the prototype chain

        1. Inheritance in the Stuent constructor

                function Student(name,age,no){ People.call(this,name,age) .... }

        2. Inherit the prototype chain

                Student.prototype = Object.create(People.prototype)

        3. Fix the Student constructor

                 Stuent.prototype.constructor = Student

instance public methods

1.Array.prototype.max = function(){return Math.max(...this))}
All arrays will have the max method
2.String.prototype.reverse = function(){ return this.split("").reverse ().join("")}
All strings will have a reverse method
Note: Generally, do not modify the methods on the default object, and be cautious when extending

Guess you like

Origin blog.csdn.net/weixin_48345246/article/details/127972710