iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 14
0

Built-in Function

經過前幾天的介紹,我們有了Function的概念,今天 ! 我們就來隆重介紹一下Javascript已經內建好的Function,一起來認識這群Built-in Function吧 !。

Type Conversion

在程式可能常會遇到要轉換型態的問題,而其實Javascript也有一套所謂強制轉型的機制,至於這機制我們有機會在做介紹,今天就以認識轉型的Function為主吧

轉換字串

常見的字串轉換 - String(argu), toString(argu), toFixed(argu)

https://ithelp.ithome.com.tw/upload/images/20180102/20107670cJQ6XcZElK.png

  • String() - 轉換成字串,但不回傳值單純把value加上String literal 。
  • toString() - 轉換成字串,會回傳值回來,也就是有return的動作,實際上是呼叫一個function,其中會判斷值是否有效。

https://ithelp.ithome.com.tw/upload/images/20180102/20107670fgepcnibkd.png

  • toFixed() - 回傳字串,並依據參數做小數點取捨。

轉換數字

https://ithelp.ithome.com.tw/upload/images/20180102/20107670zrWMPXCOd9.png

  • Number的boolean判斷為 1 為 true, 0為false。
  • 轉換出來的數字若無意義則為NaN。

Alert & Prompts

這兩個是與使用者做互動的,也可以是某些怪網站常看到爛招數,也就是彈出視窗。

Alert - 彈出訊息
https://ithelp.ithome.com.tw/upload/images/20180102/20107670ccRqbkycpT.png
https://ithelp.ithome.com.tw/upload/images/20180102/20107670sWQOWi75hf.png

Prompts - 讓使用者輸入
https://ithelp.ithome.com.tw/upload/images/20180102/20107670nCiWMIDCut.png
https://ithelp.ithome.com.tw/upload/images/20180102/20107670JwIC52H6GD.png

終極密碼

透過這兩個簡單的built-in function就可以做出簡易的終極密碼遊戲囉,可以參照附圖的程式碼試玩一下一次。
https://ithelp.ithome.com.tw/upload/images/20180102/20107670fsHJWb0yex.png

Time Controllers

setInterval(function, msecs) - 每毫秒呼叫給入的function
clearInterval(function) - 停止自動呼叫
https://ithelp.ithome.com.tw/upload/images/20180102/20107670FAbdH8zD0u.png
https://ithelp.ithome.com.tw/upload/images/20180102/20107670ZoHmbb70mL.png
https://ithelp.ithome.com.tw/upload/images/20180102/2010767001tN68Yb9j.png

JSON

很多時候在前端我們用透過JSON檔傳送資料,JSON的轉換透過下列兩個function。
JSON.stringify(); - 將想要傳輸的物件變成JSON字串形式。
JSON.parse(); - 將JSON檔案將字串轉化成Object格式。
至於這個我就不做範例了,大家可以去串看看API或者自己無聊打包JSON看看。


還有許多如之前介紹的Array內建函式,都是built-in function,而包含字串,數字運算等都已經有很多配好的,今天小弟就偷懶只簡單介紹以上幾種,大家有興趣或者無聊都歡迎去MDN找來看或者其他W3C都有介紹額。

參考來源

  1. eloquentjavascript

上一篇
Day13_Funciton嗨起來
下一篇
Day15_HohohoHohoho
系列文
那些年,我們一起錯過的Javascript30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言