第二题
作者:
kafya | 来源:发表于
2021-09-12 23:45 被阅读0次
请写出如下的输出值,并写出把注释掉的代码取消注释的值,并解释为什么
this.a = 20;
var test = {
a: 40,
init: () => {
console.log(this); // window
console.log(this.a); // 20
function go() {
// this.a = 60;
console.log(this.a); // 50
}
go.prototype.a = 50;
return go;
},
};
// var p = test.init();
// p();
new (test.init())();
- this指向的问题,this只有在执行的时候才能确认,定义的时候不能确认,
call
, apply
, bind
还有es6的箭头函数可以改变this的指向
- 原型链基础,当一个函数当做类使用的时候,原型链里
this
对属性的赋值优先级要低于构造函数内部的指向。
本文标题:第二题
本文链接:https://www.haomeiwen.com/subject/badxgltx.html
网友评论