关于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回答

7七月

2020-11-11

这报错报的是_this?

0
7
见信
非常感谢!
2020-11-12
共7条回复

纯正商业级微信小程序应用实战,全方位小程序特性讲解

真实数据的高质量小程序项目,学会直接工作

4782 学习 · 2125 问题

查看课程