关于ES6 Class
来源:17-2 在小程序中使用async和await

见信
2020-11-10
class slidePageModel{
constructor(nextPage,prevPage){
this.nextPage = nextPage
this.prevPage = prevPage
// 获取页面宽度
this._pageWidth = this._getPageWidth()
this._touchStatus = false
this._startX = 0
this._isLoading = false
this._timer = null
}
handleTouchStart (e) {
this._touchStatus = true
this._startX = e.touches[0].clientX
}
handleTouchEnd () {
this._touchStatus = false
this._isLoading = false
}
handleTouchMove () {
if(this._isLoading) return
if (this._touchStatus) {
if (this._timer) {
clearTimeout(this._timer)
this._timer = null
}
this._timer = setTimeout(() => {
// 左滑切页
if(this._isNext()){
this.nextPage()
}else if(this._isPrev()){
// 右滑切页
this.prevPage()
}
}, 8)
}
}
_getPageWidth () {
return document.documentElement.clientWidth == 0 ?
document.body.clientWidth :
document.documentElement.clientWidth
}
_isNext(){
return this._startX-e.touches[0].clientX>this._pageWidth*0.4?true:false
}
_isPrev(){
return e.touches[0].clientX-this._startX>this._pageWidth*0.4?true:false
}
}
export default slidePageModel
看了七月老师关于编程业务处理这块,从未用过ES6class的我写了个DEMO试了试,报的错误让我感到很迷惑
我在handleTouchMove函数里使用typeof检测了三个带下划线的私有方法(虽然js的私有方法只是个摆设),它报的都是undefined,包括_getPageWidth(),而根据new出来的对象,_pageWidth成功求值,并且在原型里,这三个私有方法也都是function,感觉迷上加迷,求解为什么这样
写回答
1回答
-
这报错报的是_this?
072020-11-12
相似问题