2017.03.03
- 計劃
- 實際完成
- 完善hM側邊欄組件化
- 將彈出框也封裝
- 總結
Cannot call a class as a function at _classCallCheck
babel之class處理Step1: 定義
Class Person{} 被編譯為:
function _classCallCheck(instance, Constructor) {
// 檢查是否成功創建了一個對象
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var Person = function Person() { _classCallCheck(this, Person); };
```
你可能會一頭霧水,_classCallCheck是什么?其實很簡單,它是為了保證調用的安全性。
比如我們這么調用:
// ok ` new p = new Person();`
是沒有問題的,但是直接調用:
// Uncaught TypeError: Cannot call a class as a function Person();
就會報錯,這就是_classCallCheck所起的作用。具體原理自己看代碼就好了,很好理解。
我們發現,Class關鍵字會被編譯成構造函數,于是我們便可以通過new來實現實例的生成。
Step2:Constructor探秘
我們這次嘗試加入constructor,再來看看編譯結果:
class Person() { constructor(name) { this.name = name; this.type = 'person' } }
編譯結果:
var Person = function Person(name) { _classCallCheck(this, Person); this.type = 'person'; this.name = name; };
看上去棒極了,我們繼續探索。
- class Menu {}
若在object中 { menu: Menu },調用會報錯。class不能以對象形式調用,雖然會解析成function Menu () {}。
只能通過new Menu() 去調用。
參考:寫一個微信小程序自定義公共組件
內調用class 是 App({ LoginPannel })
new app.LoginPannel()- template 使用展開的數據是
data="{{ ...obj }}"
, 若page.data
上沒有定義 obj ,在template 內使用wx:for循環輸出列表會報錯。就算在onLoad事件上setData也是有誤的。
VM18377:2 firstRender not the data from Page.data
firstRender not the data from Page.data;firstRender not the data from Page.data Error: firstRender not the data from Page.data