先說緣由:api
這麼寫會報錯函數
thirdScriptError
this.setData is not a function;at pages/index/index onLoad function;at api getSystemInfo success callback function
TypeError: this.setData is not a functionthis
onLoad: function () { wx.getSystemInfo({ success: function (res) { this.setData({ lang: res.language }) console.log(res.language) } })
這麼改一下就不報錯了。es5
1 onLoad: function () { 2 wx.getSystemInfo({ 3 success: (res) => { 4 this.setData({ 箭頭函數的this始終指向函數定義時的this 5 lang: res.language 6 7 }) 8 console.log(res.language) 9 } 10 })
或者這樣:spa
onLoad: function () { var that=this; wx.getSystemInfo({ success: function (res) { that.setData({ lang: res.language }) console.log(res.language) } })
'use strict'; var obj = { i: 10, b: () => console.log(this.i, this), c: function() { console.log(this.i, this); } } obj.b(); // prints undefined, Window {...} (or the global object) obj.c(); // prints 10, Object {...}