function中this是使用时的对象
=>中的this是定义时的对象
就像这样
onLoad: function(options) {
const eventChannel = this.getOpenerEventChannel()
eventChannel.on('acceptDataFromOpenerPage', data=>{
console.log(data)
this.setData({
singerData: data
})
})
this.getSingerDetail()
},
getSingerDetail:()=>{
// 这里打印的this是undefined,因为=>中的this指向的是定义时所在的作用域的 this值,而不会指向window
console.log(this);
},
//若把它改成这样,则这里的this指向的window,this.data输出的是window下data的数据信息
getSingerDetail:function(){
// 这里打印的是上面数据层的data,即window下data的数据
console.log(this.data);
},