小編剛剛接觸小程序,所有東西全部看文檔,多虧微信小程序的文檔比較良心(Chinese),給我們帶來了很大的遍歷。
但是,在閱讀“數據類型”時卻遇到了問題。。。
對於英文並不好的我,雖然在其他語言中有遇到這些方法,但是還是需要謹慎操作。
下面我就對這些數據類型的方法進行一些“翻譯”,並做示例說明一下
具體講解請參考 小程序說明鏈接,方法詳細講解請往下看....
WXS 語言目前共有以下幾種數據類型:
number
: 數值string
:字符串boolean
:布爾值object
:對象function
:函數array
: 數組date
:日期regexp
:正則
【number】類型所擁有的方法
toString() 直接轉成 string 類型
let num = 152.65; console.log(typeof num); //number console.log(typeof num.toString()); //string
toLocaleString() 當數字長度大於3位之后,就會用","逗號分隔
let num = 6953352; console.log(num.toLocaleString()); //6,953,352 console.log(typeof num.toLocaleString()); //string
valueOf() 將值從原生對象中取出來
let num = 56981; console.log(num.valueOf()); //56981 console.log(typeof num.valueOf()); //number
toFixed() 將浮點型四舍五入,括號中是幾就是小數點后幾位進行保留
let num = 569.49; console.log(num.toFixed(1)); //569.5 num = 569.51; console.log(num.toFixed()); //570
toExponential() 把值轉換成指數計數
let num = 569.49; console.log(num.toExponential()); //5.6949e+2
toPrecision() 在值超出指定位數時將其轉換為指數計數法
let num = 569.49; console.log(num.toPrecision(1)); //6e+2
【string】類型所擁有的方法
toString() 轉成 string 類型
valueOf() 將值從原生對象中取出來
charAt() 返回指定位置的字符(下標從 0 開始)
let str = 'hello'; console.log(str.charAt(4)); //o