js中的String其實不單單是"foo"這樣的字面量字符串。
Blob構造函數的入參array,數組元素能夠是USVString,到底什麼是USVString讓我很困惑。css
除了String外,其實還包括如下幾種類型的String。
工做中除了String.prototype上的那些好用的方法,es6的模板字符串等等,貌似也沒有其餘經常使用字符串的地方了。這裏就再也不贅述。html
參考mdn文檔和EcmaScript規範,再結合實際開發中的經驗,作一次簡單的專項學習。前端
<script>
的charset=「utf-8」怎麼理解<script>
的charset=「utf-8」不矛盾嗎U+FFFD
(�)字符)<script>
的charset="utf-8"怎麼理解規範中的說明以下:node
If the script element has a charset attribute, then let encoding be the result of getting an encoding from the value of the charset attribute.
If the script element does not have a charset attribute, or if getting an encoding failed, let encoding be the same as the encoding of the script element's node document.
To get an encoding from a string label, run these steps:
Remove any leading and trailing ASCII whitespace from label.
If label is an ASCII case-insensitive match for any of the labels listed in the table below, return the corresponding encoding, and failure otherwise.
Name | Labels |
---|---|
UTF-8 | "unicode-1-1-utf-8","utf-8","utf8" |
UTF-16LE | "utf-16","utf-16le" |
js中存在utf-8 encoder和utf-8 decoder專門進行utf-8的編解碼工做。git
<script>
的charset=「utf-8」不矛盾嗎不矛盾。utf-16人類友好,utf-8機器友好。
寫js代碼時,utf-16人類友好。人類可識別。
script utf-8編碼時utf-8友好;端到端通訊時,utf-8機器友好。機器高效運行。es6
script編碼難道不對utf-16的js string進行編碼?
編碼。可是js代碼中不僅有字符串類型,還有Boolean,Number等等一系列類型。不矛盾!github
4.3.17String value
primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer values
NOTE
A String value is a member of the String type. Each integer value in the sequence usually represents a single 16-bit unit of UTF-16 text. However, ECMAScript does not place any restrictions or requirements on the values except that they must be 16-bit unsigned integers.
經過encodeURIComponent和decodeURIComponent能夠初見端倪。
首先明確一點。
utf-8格式url(機器友好):"http://foo.test.go.com/index.html#/?from=http%3A%2F%2Fbar.crm.test.go.com%2F&redirectUrl=http%3A%2F%2Fbaz.test.go.com%2Fuser%2FgetCASUser&platformCode=10004"
utf-16格式url(人類友好):"http://foo.test.go.com/index.html#/?from=http://bar.crm.test.go.com/&redirectUrl=http://baz.test.go.com/user/getCASUser&platformCode=10004"
web
encodeURIComponent(uriComponent) 將UTF-16編碼的url(其實就是js中的url字符串,「https://www.foo.com?foo=123」)
編碼爲UTF-8格式"https%3A%2F%2Fwww.foo.com%3Ffoo%3D123"
decodeURI(encodedURIComponent)將UTF8格式的url 解碼爲utf-16格式「https://www.foo.com?foo=123」
chrome
由於:segmentfault
Note that encodeURI by itself cannot form proper HTTP GET and POST requests, such as for XMLHTTPRequests, because "&", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. encodeURIComponent, however, does encode these characters.
不能生成用於HTTP GET或者POST請求的url,由於:
encodeURI Not Escaped: A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
<script>
的charset="utf-8"沒有必要再顯式設置,會繼承document的編碼方式期待和你們交流,共同進步,歡迎你們加入我建立的與前端開發密切相關的技術討論小組:
- SegmentFault技術圈:ES新規範語法糖
- SegmentFault專欄:趁你還年輕,作個優秀的前端工程師
- 知乎專欄:趁你還年輕,作個優秀的前端工程師
- Github博客: 趁你還年輕233的我的博客
- 前端開發QQ羣:660634678
- 微信公衆號: 人獸鬼 / excellent_developers
努力成爲優秀前端工程師!