ES6使用實踐總結

【1】ES6中實例對象不能調用靜態方法

Java中其實是可以的,雖然IDEA會提示但是編譯器會通過。

如下所示,封裝一個Toast類(這裏以小程序中使用爲例):

export default class Toast{
    static showToLogin( title, duration) {
        wx.showToast({
            title: title?title:'請先登錄!',
            duration: duration ? duration : 2000,
            mask: true,
            image: "/images/icon/fail.png",
            success: function () {
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/my/my',
                })
              }, duration ? duration : 2000,);
            }
          });
    }
  
   static show(icon, title, duration) {
        if (icon === 'loading') {
            wx.showToast({
                title: title?title:'加載中...',
                icon: 'loading',
                duration: duration ? duration : 2000,
            })
        } else if (icon === 'success') {
            wx.showToast({
                title: title?title:'請求成功!',
                icon: 'success',
                duration: duration ? duration : 2000,
            })
        } else if (icon === 'warn') {
            wx.showToast({
                title: title,
                duration: duration ? duration : 2000,
                image: '../images/icon/fail.png'
            })
        } else if (icon === 'fail') {
            wx.showToast({
                title: title?title:'請求失敗!',
                duration: duration ? duration : 2000,
                image: '../images/icon/fail.png'
            })
        }
    }
}

index.js中引入並測試:

//index.js
import Toast from "../../utils/toast.js";
let toast=new Toast();

  onLoad: function () {
    toast.show('loading',null,null);
    // Toast.showToLogin(null,null);
}

Toast.showToLogin是ok的,類調用靜態方法,java中也一樣。但是toast.show('loading',null,null);就會報錯,如下提示:
在這裏插入圖片描述
statis show改爲show ,也就是非靜態方法則OK!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章