JavaScript this关键字拾遗

2019-03-12  本文已影响0人  骑着乌龟追小兔

this关键字拾遗

this:调用函数时使用的引用,决定了函数执行时候的this值

采用箭头函数,无论什么引用调用,都不会改变this值

this 关键字的机制

js标准为函数规定了用来保存定义了上下文的属性[Environment]

当函数执行时候会创建一条心的执行环境记录,记录外层词法环境(outer lexical environment)会被设置成[Environment]

strict 模式下this的行为

this 严格按照调用时传入的值,可能为 null 或者 undifined

Function操作this的内置函数

apply(this: Function, thisArg: any, argArray?: any): any;

call(this: Function, thisArg: any, ...argArray: any[]): any;

这两个函数用于指定函数调用时传入的 this 值,区别在于 apply 只能传入一个参数

bind(this:Function,thisArg:any,...argArray:any[]):any;

bind 在绑定this,同时指定了参数,需要手动调用

this与new 关键字

new只能调用普通函数 和 普通方法

上一篇 下一篇

猜你喜欢

热点阅读