JavaScript高階程式設計之基本引用型別
目錄
- 1、Date
- 2、RegExp
- 3、原始包裝型別
前言:
引用值(物件)都是某個特定的引用型別的例項物件,例如 Date
型別、Function
型別,RegExp
正則表示式型別等等。內建了一些常用的引用型別給開發者使用。
1、Date
下面3個方法返回的單位為毫秒的時間戳
let t1 = Date.UTC(2020,11,7,22,14) let t2 = Date.parse("5/23/2020") let t3 = Date.now()
parse 接收的引數格式可以為:
Date()
建構函式接收的引數有很多種,可以不接收任何引數,也可以接收時間戳,還可以是UTC
函式和parse函式接收的引數
let t4 = new Date() let t5 = new Date(2020,7) // 月份從0開始 let t6 = new Date("5/23/2020")
Date
型別提供了相當於的函式,可自行查閱相關API
。
2、RegExp
Script
中,正則表示式使用如下方式表示
let express = /pattern/flags;
- g :全域性模式,表示查詢字串的全部內容,而不是找到第一個匹配的內容就結束。
- i :不區分大小寫,表示在查詢匹配時忽略 pattern 和字串的大小寫。
- m :多行模式,表示查詢到一行文字末尾時會繼續查詢。
- u :Unicode 模式,啟用 Unicode 匹配。
- s : dotAll 模式,表示元字元 . 匹配任何字元(包括 \n 或 \r )。
除了用字面量形式表示正則表示式之外,還可以使用RegExp建構函式建立。
let p1 = /[cb]at/i let p2 = new RegExp("[cb]at","i")
兩者等效。
RegExp
物件有兩個重要的方法,分別是exec
和test
方法
let p1 = /[cb]at/i let text = "hello cat bat nihao" matches = p1.exec(text) console.log(matches.index) console.log(matches.input) console.log(matches[0]) //cat
如果正則表示式匹配的話,返回的 matches
是Array
陣列物件, 不匹配返回的就是null。 另外 matches
還有兩個額外的屬性 index
和input
。 index 是匹配的其實位置。input
就是要查詢的字串。 數組裡面的元素其實就是正則表示式中的捕獲分組。如果沒有分組,第0個元素就是匹配的那個字串。
let p1 = /([cb]a(t))/i console.log(matches[0]) //cat console.log(matches[1]) //cat console.log(matches[2]) //t
test 方法返回的是布林值,用於判斷字串是否匹配正則表示式。
let matched = /\d+/g.test("13") console.log(matched) //true
這裡還要解釋一下flag
裡面的那個g的作用, 現在看起來好像沒什麼用,看下面這個例子YbzOb就能明白
let p1 = /[cb]at/gi let text = "hello cat bat nihao" console.log(p1.exec(text)) // ["cat",index: 6,input: "hello cat bat nihao",groups: undefined] console.log(p1.exec(text)) //["bat",index: 10,groups: undefined]
text 這個字串中,其實有兩處是匹配正則表示式的,但是我們呼叫exec
的適合,matches
數組裡面的元素是cat,並沒有bat。 當我第二次執行exec
的適合,返回的就是bat了。如果沒有g,不管呼叫多少次,返回的都是cat。找到第一個匹配的內容就結束了。
另外,字串提供了很多方法,接收正則表示式作為引數,可以用於正則表示式匹配,字串替換、查詢指定字串或則分組。
3、原始包裝型別
Boolean
、Number
、String
這三個引用型別是原始型別對應的包裝型別, 當在原始值身上用到某些方法時,會自動將原始值包裝成對應的引用型別。比如上面字串提供的那些方法,原始值本來是沒有這些方法的,但是String
型別就有。 這種機制使得原始值也能擁有物件的行為。
原始包裝型別有如下特點,可以注意下:
let s1 = "hello" s1.name = "sex" console.log(s1.name) //undefined
為什麼s1.name
是undefined
呢? 執行第二行程式碼的時候,會將s1轉換為了包裝型別, 執行完這行程式碼時,該型別的物件其實就被回收了。然後在第三行又建立了一個包裝型別的物件,但是這個時候並沒有name
屬性,所以就是undefined
。 除非顯示的建立一個String
物件。
let s1 = "hello" s1 = new String(s1) s1.name = "sex" console.log(s1.name)
對於Number
, 使用 new
和 不適用 new
的區別
let value = 25 let number = Number(value) obj = new Number(value) console.log(typeof number) //number console.log(typeof obj) //object console.log(number instanceof Number) //false 原始值不是任何型別的例項物件 console.log(obj instanceof Number) //true
number
是原始值, obj
是Number
型別的物件。
Number
物件有個to客棧Fxied
方法用於將數值格式化為字串, 用於保留指定位小數
let num = 10.007 console.log(num.toFixed(2)) //10.01
對於String,提供了非常多的操作字串的方法,可以按照例項全部手敲一遍。
4、其他內建物件
全域性作用域下面的變數,常用的一些單獨的函式如evel
,parseInt
,isNaN
,還有原始引用型別的建構函式 String
、Boolean
、Number Function
、Array
都是Global
的屬性。但是我們沒法顯示的訪問Global
物件。另外encodeURI
,encodeURIComponent
在處理URL的適合非常有用。
url = "http://www.baidu.com/. " console.log(encodeURI(url)) //http://www.baidu.com/vue.%20js console.log(encodeURIComponent(url)) // http%3A%2F%2Fwww.baidu.com%2Fvue.%20js
與之對應的解碼函式是decodeURI
,decodeURIComponent
。
在瀏覽器中,window
物件作為了Global的代理物件,全域性作用域下面的變數可以通過window
物件來訪問。
var name = "zhang" console.log(window.name) Math物件 console.log(Math.max(1,2,3,4)) // 最大值 console.log(Math.min(3,4)) // 最小值 console.log(Math.ceil("1.2")) //向上取整 console.log(Math.floor(3.3)) //向下取整 console.log(Math.round(3.6)) //四捨五入取整
到此這篇關於JavaScript高階程式設計之基本引用www.cppcns.com型別的文章就介紹到這了,更多相關JavaScrip基本引用型別內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!