js内置对象及用法

50 阅读3分钟

String

String对字符串的支持
String.charAt( )返回字符串中的第n个字符
String.charCodeAt( )返回字符串中的第n个字符的代码
String.concat( )连接字符串
String.indexOf( )检索字符串
String.slice( )抽取一个子串
String.split( )将字符串分割成字符串数组

String.charAt(n)    参数:索引n -->返回值:n下标对应元素   n>length  返回空值

String.charCodeAt(n)   参数:n --> 返回值:n对应的Unicode编码

String.concat(value,......)    参数:字符串 --->返回值:连接起来

String.indexOf( substring[,start] )

substring要在字符串string中检索的子串。start一个可选的整数参数,声明了在字符串String中开始检索的位置。它的合法取值是0(字符串中的第一个字符的位置)到string.length-1(字符串中的最后一个字符的位置)。如果省略了这个参数,将从字符串的第一个字符开始检索。返回值如果在string中的start位置之后存在substring返回出现的第一个substring 的位置。如果没有找到子串substring返回-1。

| ``` string.slice(start, end)

| -------------------------------------------------------- |
| 一个新字符串,包括字符串string从start开始(包括start)到end为止(不包 括end)的所有字符。 |

| ```
string.split(delimiter, limit)  
```[]()参数*delimiter*字符串或正则表达式,从该参数指定的地方分割string。ECMAScript V3标准化了正则表达式作为定界符使用时的用法,JavaScript 1.2和JScript 3.0实现了它。JavaScriptl.1没有实现它。*limit*这个可选的整数指定了返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数字。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。ECMAScriptv3标准化了该参数,JavaScript 1.2和JScript 3.0实现了它。JavaScript 1.1没有实现它。[]()返回值一个字符串数组,是通过在delimiter指定的边界处将字符串string分割成子串创建的。返回的数组中的子串不包括delimiter自身,但下面列出的情况除外。 |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

## 数组方法

| [Array.join( )](ch23-54-fm2xml.html)                                                   | 将数组元素连接起来以构建一个字符串 |
| -------------------------------------------------------------------------------------- | ----------------- |
| ```
a = new Array(1, 2, 3, "testing");  s = a.join("+");  // s 是字符串"1+2+3+testing"
``` |                   |

### 栈结构方法

| [Array.pop( )](ch23-64-fm2xml.html)                          | 删除并返回数组的最后一个元素 |
| ------------------------------------------------------------ | -------------- |
| [Array.push( )](ch23-70-fm2xml.html)                         | 给数组添加元素        |
| 参数*value, ...*要添加到array尾部的值,可以是一个或多个。[]()返回值把指定的值添加到数组后的新长度。 |                |

### 队列方法

| [Array.shift( )](ch23-81-fm2xml.html)                                                    | 将元素移出数组   返回数组原来的第一个元素 |
| ---------------------------------------------------------------------------------------- | ---------------------- |
| ```
var a = [1, [2,3], 4]  a.shift(  );  // 返回 1; a = [[2,3], 4] 
```                    |                        |
| [Array.unshift( )](ch23-122-fm2xml.html)                                                 | 在数组头部插入一个元素   返回数组的新长度 |
| ```
var a = [];             // a:[]  a.unshift(1);           // a:[1]          返回 1 
``` |                        |

## Data

| [Date](ch23-144-fm2xml.html)                    | 操作日期和时间的对象    |
| ----------------------------------------------- | ------------- |
| [Date.getDate( )](ch23-152-fm2xml.html)         | 返回一个月中的某一天    |
| [Date.getDay( )](ch23-155-fm2xml.html)          | 返回一周中的某一天     |
| [Date.getFullYear( )](ch23-158-fm2xml.html)     | 返回Date对象的年份字段 |
| [Date.getHours( )](ch23-161-fm2xml.html)        | 返回Date对象的小时字段 |
| [Date.getMilliseconds( )](ch23-164-fm2xml.html) | 返回Date对象的毫秒字段 |
| [Date.getMinutes( )](ch23-167-fm2xml.html)      | 返回Date对象的分钟字段 |
| [Date.getMonth( )](ch23-170-fm2xml.html)        | 返回Date对象的月份字段 |
| [Date.getSeconds( )](ch23-173-fm2xml.html)      | 返回Date对象的秒字段  |
| [Date.getTime( )](ch23-176-fm2xml.html)         | 返回Date对象的毫秒表示 |

 

###