JS

44 阅读1分钟

哪些类型是值类型?哪些类型是引用类型

  • 值类型:String、Number、Boolean、Symbol
  • 引用类型:JSON、Array、null

演示值类型与引用类型的堆栈模型

image.png

image.png

  • const a={x:100, y:200}
  • const b=a
  • let c=a.x
  • b.x=300
  • console.log(a) // x:300

如何实现深拷贝

JSON.parse(JSON.stringify(obj)) 不能存放函数、时间对象、正则... 递归 没有考虑循环引用。。 lodash.cloneDeep 推荐,工作中没必要重复造轮子

何时使用==何时使用===

== 隐式类型转换

  • 100 == '100'
  • 0 == ''
  • 0 == false
  • false == ''
  • null == undefined

只有 obj == null 使用双等

if (obj === null || obj === undefined) {

}

  • console.log(100 == "100");
    
  • console.log(0 == "");
    
  • console.log(0 == false);
    
  • console.log("" == false);
    
  • console.log(null == undefined);
    
  • let obj = { age: 18 };
    
  • if (obj == null) {
    
  • }
    
  • if (obj === null || obj === undefined) {
    
  • }
    

### 什么是truly变量?什么是falsely变量?

  • truly变量:!!val === true
  • falsely变量:!!val === false

除了falsely变量,都是truly变量

  • 数字0
  • NaN
  • 空字符串
  • null
  • undefined

console.log(!!"" === false);
console.log(!!NaN === false);
console.log(!!0 === false);
console.log(!!null === false);
console.log(!!undefined === false);

原型和原型链是怎么回事

复习class基础语法

  • class
  • construct
  • extends
  • super
  • instanceof

什么是原型

image.png

什么是原型链

image.png

  • class Person {
  •   constructor(name) {
    
  •     this.name = name;
    
  •   }
    
  •   sayHi() {
    
  •     console.log(`${this.name} say hi`);
    
  •   }
    
  • }
    
  • let zhangsan = new Person("张三");
    
  • // zhangsan.sayHi();
    
  • // console.log(zhangsan instanceof Person);
    
  • console.log(zhangsan instanceof Object);
    
  • console.log(Person instanceof Object);