-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前端String那些事儿 #163
Comments
USVString
DOMString
CSSOMString
Binary strings
|
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的编解码工作。
js中的String采用utf-16格式编码与 <script>
的charset=“utf-8”不矛盾吗
不矛盾。utf-16人类友好,utf-8机器友好。
写js代码时,utf-16人类友好。人类可识别。
script utf-8编码时utf-8友好;端到端通信时,utf-8机器友好。机器高效运行。
script编码难道不对utf-16的js string进行编码?
编码。但是js代码中不只有字符串类型,还有Boolean,Number等等一系列类型。不矛盾!
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.
- js字符串中是由0个或者多个16bit的无符号整数组成
- 每个整数的值通常表示UTF-16文本的一个16bit单元
- es规定js字符必须是一个16bit的无符号整数
初见端倪
通过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"
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”
为什么不用encodeURI?
因为:
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 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
小结
- utf-8编码机器友好:浏览器http url,script默认编码格式等等
- utf-16编码人类友好:肉眼可识别字符串
- script utf-8编码难道不对utf-16的js string进行编码? 编码。但是js代码中不只有字符串类型,还有Boolean,Number等等一系列类型。不矛盾!
总结
|
js中的String其实不仅仅是"foo"这样的字面量字符串。
Blob构造函数的入参array,数组元素可以是USVString,到底什么是USVString让我很困惑。
除了String外,其实还包括以下几种类型的String。
工作中除了String.prototype上的那些好用的方法,es6的模板字符串等等,貌似也没有其他常用字符串的地方了。这里就不再赘述。
参考mdn文档和EcmaScript规范,再结合实际开发中的经验,做一次简单的专项学习。
<script>
的charset=“utf-8”怎么理解<script>
的charset=“utf-8”不矛盾吗The text was updated successfully, but these errors were encountered: