Skip to content

Latest commit

 

History

History
124 lines (91 loc) · 2.61 KB

10.md

File metadata and controls

124 lines (91 loc) · 2.61 KB

十、附录 b:内置函数

本附录包含内置函数(全局对象的方法)列表,在第 3 章中讨论。

|

功能

|

描述

| | --- | --- | | parseInt() | 取两个参数:输入对象和基数;然后尝试返回输入的整数表示。不处理输入中的指数。默认基数为 10(十进制数)。失败时返回 NaN。省略基数可能会导致意想不到的结果(例如 08 这样的输入),因此最好总是指定它。

>>>parseInt('10e+3')
10
>>> parseInt('FF')
NaN
>>> parseInt('FF', 16)
255

| | parseFloat() | 接受一个参数,并尝试返回它的浮点数表示形式。理解输入中的指数。

>>> parseFloat('10e+3')
10000
>>> parseFloat('123.456test')
123.456

| | isNaN() | 缩写自“不是数字”。接受参数,如果参数不是有效数字,则返回真,否则返回false。首先尝试将输入转换为数字。

>>> isNaN(NaN)
true
>>> isNaN(123)
false
>>> isNaN(parseInt('FF'))
true
>>> isNaN(parseInt('FF', 16))
false

| | isFinite() | 如果输入的是数字(或者可以转换成数字),但不是数字Infinity or -Infinity,则返回真。对于无穷大或非数值,返回false

>>> isFinite(1e+1000)
false
>>> isFinite(-Infinity)
false
>>> isFinite("123")
true

| | encodeURIComponent() | 将输入转换为 URL 编码的字符串。关于网址编码工作原理的更多细节,请参考维基百科文章:http://en.wikipedia.org/wiki/Url_encode

>>>encodeURIComponent
('http://phpied.com/')
"http%3A%2F%2Fphpied.com%2F"
>>>encodeURIComponent
('some script?key=v@lue')
"some%20script%3Fkey%3Dv%40lue"

| | decodeURIComponent() | 获取一个 URL 编码的字符串并对其进行解码。

>>> decodeURIComponent('%20%40')
" @"

| | encodeURI() | URL-对输入进行编码,但假设给出了完整的 URL,因此通过不对协议(例如http://)和主机名(例如 www.phpied.com)进行编码来返回有效的 URL。

>>> encodeURI('http://phpied.com/')
http://phpied.com/
>>> encodeURI('some script?key=v@lue')
"some%20script?key=v@lue"

| | decodeURI() | 与 encodeURI()相反。

>>>decodeURI("some%20script?key=v@lue")
"some script?key=v@lue"

| | eval() | 接受一串 JavaScript 代码并执行它。返回输入字符串中最后一个表达式的结果。尽可能避免。

>>> eval('1+2')
3
>>> eval('parseInt("123")')
123
>>> eval('new Array(1,2,3)')
[1, 2, 3]
>>> eval('new Array(1,2,3); 1+1;')
2

|