07-常用内置对象
数组array
1.数组的创建方式
字面量方式创建:
var colors = ['red', 'green', 'yellow'];
使用构造函数的方式创建(使用new关键字对构造函数进行创建对象)
var colors2 = new array();
2.数组的赋值
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var color = []; 12 color[0] = 'red'; 13 color[1] = 'green'; 14 color[2] = 'yellow'; 15 console.log(color); 16 </script> 17 18 </body> 19 </html>
3.数组的常用方法
3.1 concat:把几个数组合并成一个数组
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var north = ['北京', '山东', '天津']; 12 var south = ['东莞', '深圳', '上海']; 13 var newcity = north.concat(south); 14 console.log(newcity); 15 </script> 16 17 </body> 18 </html>
3.2 join:将数组中的元素使用指定的字符串连接起来,他会形成一个新的字符串
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var score = [78, 89, 48, 20, 100]; 12 var str = score.join('*'); 13 console.log(str); 14 </script> 15 16 </body> 17 </html>
3.3 将数组转换成字符串tostring()
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var score = [78, 89, 48, 20, 100]; 12 var str = score.tostring(); 13 console.log(str); 14 </script> 15 16 </body> 17 </html>
3.4 slice(start,end):返回数组的一段,左闭右开
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var score = [78, 89, 48, 20, 100]; 12 var nscore = score.slice(1, 4); 13 console.log(nscore); 14 </script> 15 16 </body> 17 </html>
3.5 pop:删除数组的最后一个元素,并返回删除的元素
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 var item = arr.pop(); 13 console.log(arr); 14 console.log(item); 15 </script> 16 17 </body> 18 </html>
3.6 push():向数组末尾添加一个或多个元素,并返回新的长度
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 var item = arr.push('帅哥'); 13 var item1 = arr.push('美女', '野兽'); 14 console.log(arr); 15 console.log(item); 16 console.log(item1); 17 </script> 18 19 </body> 20 </html>
3.7 reverse():翻转数组
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 arr.reverse(); 13 console.log(arr); 14 </script> 15 16 </body> 17 </html>
3.8 sort():对数组排序
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 arr.sort(); 13 console.log(arr); 14 </script> 15 16 </body> 17 </html>
3.9 isarray():判断是否为数组
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 console.log(array.isarray(arr)); 13 </script> 14 15 </body> 16 </html>
3.10 shift():删除并返回数组的第一个元素
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 var item = arr.shift(); 13 console.log(arr); 14 console.log(item); 15 </script> 16 17 </body> 18 </html>
3.11 unshift():向数组的开头添加一个或多个元素,并返回新的长度
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 var lengs = arr.unshift('老大', '老二'); 13 console.log(arr); 14 console.log(lengs); 15 </script> 16 17 </body> 18 </html>
3.12 length:唯一的属性,返回数组的长度
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = ['张三', '李四', '王文', '找六']; 12 console.log(arr.length); //4 13 </script> 14 15 </body> 16 </html>
字符串string
1.chartat():返回指定索引位置的字符
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'xiaoshuaige'; 12 var chset = str.charat(3); 13 console.log(chset); //o 14 </script> 15 16 </body> 17 </html>
2.concat():返回两个或多个字符串拼接后的字符串值
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str1 = 'al'; 12 var str2 = 'vin'; 13 console.log(str1.concat(str2, str2, str2)); //alvinvinvin 14 </script> 15 16 </body> 17 </html>
3.replace(a,b):将字符串a替换成字符串b
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'alhdifjghjri'; 12 var newstr = str.replace('lhdi', 'hgfhg345'); 13 console.log(str); //alhdifjghjri 14 console.log(newstr); //ahgfhg345fjghjri 15 </script> 16 17 </body> 18 </html>
4.indexof():查找字符串中字符的下标,找到返回字符串的下标,找不到返回-1.和search()用法一样
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'alhdifjghjri'; 12 console.log(str.indexof('d')); //3 13 console.log(str.search('i')); //4 14 </script> 15 16 </body> 17 </html>
5.slice(start,end):提取字符串的一部分,并返回一个新的字符串,左闭右开
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'alhdifjghjri'; 12 var str1 = str.slice(2, 7); 13 console.log(str1); //hdifj 14 </script> 15 16 </body> 17 </html>
6 split('a',1) 以字符串a分割字符串,并返回新的数组。如果第二个参数没写,表示返回整个数组,如果定义了个数,则返回指定个数的数组
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = '非个人分,a已复核给a回顾二行,返回a发黑发'; 12 var str1 = str.split('a', 2); 13 var str2 = str.split('a'); 14 console.log(str); 15 console.log(str1); 16 console.log(str2); 17 </script> 18 19 </body> 20 </html>
7.substr(start,length):返回一个字符串从指定位置开始到指定字符数的字符
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = '非个人分,a已复核给a回顾二行,返回a发黑发'; 12 console.log(str.substr(2, 7)); //人分,a已复核 13 </script> 14 15 </body> 16 </html>
8.tolowercase():转小写
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'fuehfeuggrg'; 12 console.log(str.tolowercase()); //fuehfeuggrg 13 </script> 14 15 </body> 16 </html>
9.touppercase():转大写
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'fjehgeugheg'; 12 console.log(str.touppercase()); //fjehgeugheg 13 </script> 14 15 </body> 16 </html>
10.substring(indexstart,indexend) 提取字符串中介于两个指定下标之间的字符。返回的子串包括 开始 处的字符,但不包括 结束 处的字符
- 如果
indexstart
等于indexend
,substring
返回一个空字符串。 - 如果省略
indexend
,substring
提取字符一直到字符串末尾。 - 如果任一参数小于 0 或为
nan
,则被当作 0。 - 如果任一参数大于
stringname.length
,则被当作stringname.length
。 - 如果
indexstart
大于indexend
,则substring
的执行效果就像两个参数调换了一样
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = 'fjehgeugheg'; 12 console.log(str.substring(1, 6)); //jehge 13 console.log(str.substring(3, 3)); // 14 console.log(str.substring(-1, 8)); //fjehgeug 15 console.log(str.substring(9, 2)); //ehgeugh 16 console.log(str.substring(3)); //hgeugheg 17 </script> 18 19 </body> 20 </html>
11.trim():去除字符串两边的空白
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var str = ' fjehg eugheg '; 12 var str1 = str.trim(); 13 console.log(str1); //fjehg eugheg 14 </script> 15 16 </body> 17 </html>
date日期对象
创建日期对象只有构造函数一种方式,使用new关键字
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var mydate = new date(); 12 console.log(mydate.getdate()); 13 console.log(mydate); 14 console.log(mydate.getmonth() + 1); 15 console.log(mydate.tolocalestring()); 16 </script> 17 18 </body> 19 </html>
math 内置对象
1.math.ceil():向上取整,‘天花板函数’
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var x = 7.5343 12 var a = math.ceil(x); 13 console.log(a); //8 14 </script> 15 16 </body> 17 </html>
2.math.floor():向下取整,‘地板函数’
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var x = 7.5343 12 var a = math.floor(x); 13 console.log(a); //7 14 </script> 15 16 </body> 17 </html>
3.求两个数的最大值和最小值
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 console.log(math.max(5, 2)); //5 12 console.log(math.min(5, 2)); //2 13 </script> 14 15 </body> 16 </html>
4.math.random()随机数
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var ran = math.random(); 12 console.log(ran); //0.13612615468574973 13 </script> 14 15 </body> 16 </html>
计算100-300之间的一个随机数
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var ran = math.random(); 12 console.log(100 + ran*(300-100)); //215.83127756609971 13 </script> 14 15 </body> 16 </html>
作者:流浪者
日期:2019-09-04