js笔记

• undefined

  1. 判断一个值是否为空要用全等,因为两个等于号的时候 null和undefined是一样的

  2. ‘a’ in window 是指 a在全局有没有被声明

  3. void -对给定的表达式进行求值,然后返回undefined 相当于关键字 void(0)对0进行求值

  4. undefined 即是一个原始数据类型,也是一个原始值数据,是全局对象上的一个属性 window.undefined

  5. 不可写 不可配置 不可枚举 不可重新定义

• this指向

  1. 当前环境执行器上下文对象的一个属性,this在不同的环境,不同作用下,表现是不同的。

  2. js获取全局对象-window,self, frames,this 。通用的获取全局方式: globalThis

  3. calss类本质上还是函数,它是一个容器/作用域/模块/壳子。在constructor中直接给this添加的方法就是 类的非静态方法-在new的过程中给this添加的方法/属性。类的静态方法在定义的时候已经放进了类的prototype{… }属性内部..默认的情况下 一个对象一定有“proto”指向构造它的构造函数或类的原型属性. “proto”是prototype{… }的容器.“proto“是原型链的连接点..如果对象上找不到Test方法,就会顺着原型链找

4.继承, call/bind/apply

 // 继承
class Father {

  constructor () {
    // no this binding
    this.age = 44;
  }

  swim () {
    console.log('GO SWIMMING')
  }
}

class Son extends Father {

  constructor () {
    // super要放到最上面(在调用super()之前,不能去访问this)

    // super()做的事情:
    // 调用father上的son有了constructor,相当于生成了this的绑定 -> Father内部的this指向了son 的实例
    // this -> new Father() -> {}
    super(); // son有了constructor,必须要继承Father的constructor
    this.hobby= "fishing"
    console.log(this.age) // 44
  }

  study () {
    console.log(this) // 这个this里面并没有swim方法,是在this._proto_.swim里面
    this.swim(); // 这个this是由son继承它的原型来的,this指向Son 的实例
  }
}



const son = new Son();

son.study();



// call bind apply
// 让函数内的this指向固定
// bind只会生效一次
var obj = {
  a: 1
}
var obj2 = {
  a: 100
}
var a = 2
function test(b, c) {
  console.log(this.a, b, c)
}
test() // 2,undefined,undefined
test.call(obj) // 1 undefined undefined
test.apply(obj) // 1 undefined undefined

// call和apply的区别 调用方式不一样
test.call(obj, 3, 4) // 1 3 4
test.apply(obj, [3, 4]) // 1 3 4

const t1 = test.bind(obj, 3, 4).bind(obj2, 3, 4) // 1
const t2 = test.bind(obj2, 3, 4).bind(obj, 3, 4) // 100
```
复制代码

5.箭头函数

箭头函数是忽略任何形式的this指向的改变,静态this指向,箭头函数一个不是一个构造器(不可以new)。

箭头函数中的this不是谁绑定指向谁,箭头函数中的this指向非箭头函数的外层作用域。

构造函数里默认隐式返回this,或者手动返回this。如果手动返回了一个新对象,this指向的对象就被忽略了。

事件处理函数内部的this指向被绑定DOM元素

promise 异步问题同步化解决方案

Map不能用for in 遍历

for in 不应该用于迭代一个关注索引顺序的array

getter 访问一个变量的时候进行拦截

var _default = 0;
Object.defineProperty(window, 'a', {
  get () {
    return ++_default
  }
})

if (a === 1 && a === 2 && a === 3) {
  console.log('win') //win
}
 


var len1 = ({} + {}).length; // 类型转换,toString // 30
var len1 = ([] + []).length; // 类型转换,toString // 0
var len3 = (function(){}).length; // 形参个数 0
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享