JavaScript(JS)之Javascript对象BOM,History,Location,Function...(二)
https://www.cnblogs.com/haiyan123/p/7594046.html
在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String、Math、Array、Date、RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的
//利用数字对象获取可表示最大数 var a = Number.MAX_VALUE; console.log(a); //创建字符串对象 var bb = new String("Hello JS"); console.log(bb); //创建日期对象 var cc = new Date(); console.log(cc); //数组对象 var dd = new Array(1,2,3,4); console.log(dd);
1.7976931348623157e+308
String {"Hello JS"}
0: "H"1: "e"2: "l"3: "l"4: "o"5: " "6: "J"7: "S"
length: 8
__proto__:
String[[PrimitiveValue]]: "Hello JS"
Sun Mar 17 2019 07:11:00 GMT+0800 (中国标准时间)
(4) [1, 2, 3, 4]
0: 1
1: 2
2: 3
3: 4
length: 4
__proto__: Array(0)
一、string对象(字符串)
1.字符串对象创建
字符串创建(两种方式)
① 变量 = “字符串”
② 字串串对象名称 = new String (字符串)
// ======================== // 字符串对象的创建有两种方式 // 方式一 var s = 'sffghgfd'; // 方式二 var s1 = new String(' hel lo '); console.log(s,s1); console.log(typeof(s)); //object类型 console.log(typeof (s1)); //string类型
2.字符串对象的属性和函数
-------属性 x.length ----获取字符串的长度 ------方法 x.toLowerCase() ----转为小写 x.toUpperCase() ----转为大写 x.trim() ----去除字符串两边空格 ----字符串查询方法 x.charAt(index) ----str1.charAt(index);----获取指定位置字符,其中index为要获取的字符索引 x.indexOf(index)----查询字符串位置 x.lastIndexOf(findstr) x.match(regexp) ----match返回匹配字符串的数组,如果没有匹配则返回null x.search(regexp) ----search返回匹配字符串的首字符位置索引 示例: var str1="welcome to the world of JS!"; var str2=str1.match("world"); var str3=str1.search("world"); alert(str2[0]); // 结果为"world" alert(str3); // 结果为15 ----子字符串处理方法 x.substr(start, length) ----start表示开始位置,length表示截取长度 x.substring(start, end) ----end是结束位置 x.slice(start, end) ----切片操作字符串 示例: var str1="abcdefgh"; var str2=str1.slice(2,4); var str3=str1.slice(4); var str4=str1.slice(2,-1); var str5=str1.slice(-3,-1); alert(str2); //结果为"cd" alert(str3); //结果为"efgh" alert(str4); //结果为"cdefg" alert(str5); //结果为"fg" x.replace(findstr,tostr) ---- 字符串替换 x.split(); ----分割字符串 var str1="一,二,三,四,五,六,日"; var strArray=str1.split(","); alert(strArray[1]);//结果为"二" x.concat(addstr) ---- 拼接字符串
<script> // ======================== // 字符串对象的创建有两种方式 // 方式一 var s = 'sffghgfd'; // 方式二 var s1 = new String(' hel lo '); console.log(s,s1); console.log(typeof(s)); //object类型 console.log(typeof (s1)); //string类型 // ====================== // 字符串对象的属性和方法 // 1.字符串就这么一个属性 console.log(s.length); //获取字符串的长度 // 2.字符串的方法 console.log(s.toUpperCase()) ; //变大写 console.log(s.toLocaleLowerCase()) ;//变小写 console.log(s1.trim()); //去除字符串两边的空格(和python中的strip方法一样,不会去除中间的空格) //// 3.字符串的查询方法 console.log(s.charAt(3)); //获取指定索引位置的字符 console.log(s.indexOf('f')); //如果有重复的,获取第一个字符的索引,如果没有你要找的字符在字符串中没有就返回-1 console.log(s.lastIndexOf('f')); //如果有重复的,获取最后一个字符的索引 var str='welcome to the world of JS!'; var str1 = str.match('world'); //match返回匹配字符串的数组,如果没有匹配则返回null var str2 = str.search('world');//search返回匹配字符串从首字符位置开始的索引,如果没有返回-1 console.log(str1);//打印 alert(str1);//弹出 console.log(str2); alert(str2); // ===================== // 子字符串处理方法 var aaa='welcome to the world of JS!'; console.log(aaa.substr(2,4)); //表示从第二个位置开始截取四个 console.log(aaa.substring(2,4)); //索引从第二个开始到第四个,注意顾头不顾尾 //切片操作(和python中的一样,都是顾头不顾尾的) console.log(aaa.slice(3,6));//从第三个到第六个 console.log(aaa.slice(4)); //从第四个开始取后面的 console.log(aaa.slice(2,-1)); //从第二个到最后一个 console.log(aaa.slice(-3,-1)); // 字符串替换、、 console.log(aaa.replace('w','c')); //字符串替换,只能换一个 //而在python中全部都能替换 console.log(aaa.split(' ')); //吧字符串按照空格分割 alert(aaa.split(' ')); //吧字符串按照空格分割 var strArray = aaa.split(' '); alert(strArray[2]) </script>
二、Array对象(数组)
1.创建数组的三种方式
创建方式1: var arrname = [元素0,元素1,….]; // var arr=[1,2,3]; 创建方式2: var arrname = new Array(元素0,元素1,….); // var test=new Array(100,"a",true); 创建方式3: var arrname = new Array(长度); // 初始化数组对象: var cnweek=new Array(7); cnweek[0]="星期日"; cnweek[1]="星期一"; ... cnweek[6]="星期六";
2.数组的属性和方法
// ==================== // 数组对象的属性和方法 var arr = [11,55,'hello',true,656]; // 1.join方法 var arr1 = arr.join('-'); //将数组元素拼接成字符串,内嵌到数组了, alert(arr1); //而python中内嵌到字符串了 // 2.concat方法(链接) var v = arr.concat(4,5); alert(v.toString()) //返回11,55,'hello',true,656,4,5 // 3.数组排序reserve sort // reserve:倒置数组元素 var li = [1122,33,44,20,'aaa',2]; console.log(li,typeof (li)); //Array [ 1122, 33, 44, 55 ] object console.log(li.toString(), typeof(li.toString())); //1122,33,44,55 string alert(li.reverse()); //2,'aaa',55,44,33,1122 // sort :排序数组元素 console.log(li.sort().toString()); //1122,2,20,33,44,aaa 是按照ascii码值排序的 // 如果就想按照数字比较呢?(就在定义一个函数) // 方式一 function intsort(a,b) { if (a>b){ return 1; } else if (a<b){ return -1; } else{ return 0; } } li.sort(intsort); console.log(li.toString());//2,20,33,44,1122,aaa // 方式二 function Intsort(a,b) { return a-b; } li.sort(intsort); console.log(li.toString()); // 4.数组切片操作 //x.slice(start,end); var arr1=['a','b','c','d','e','f','g','h']; var arr2=arr1.slice(2,4); var arr3=arr1.slice(4); var arr4=arr1.slice(2,-1); alert(arr2.toString());//结果为"c,d" alert(arr3.toString());//结果为"e,f,g,h" alert(arr4.toString());//结果为"c,d,e,f,g" // 5.删除子数组 var a = [1,2,3,4,5,6,7,8]; a.splice(1,2); console.log(a) ;//Array [ 1, 4, 5, 6, 7, 8 ] // 6.数组的push和pop // push:是将值添加到数组的结尾 var b=[1,2,3]; b.push('a0','4'); console.log(b) ; //Array [ 1, 2, 3, "a0", "4" ] // pop;是讲数组的最后一个元素删除 b.pop(); console.log(b) ;//Array [ 1, 2, 3, "a0" ] //7.数组的shift和unshift //unshift: 将值插入到数组的开始 //shift: 将数组的第一个元素删除 b.unshift(888,555,666); console.log(b); //Array [ 888, 555, 666, 1, 2, 3, "a0" ] b.shift(); console.log(b); //Array [ 555, 666, 1, 2, 3, "a0" ] // 8.总结js的数组特性 // java中的数组特性:规定是什么类型的数组,就只能装什么类型.只有一种类型. // js中的数组特性 // js中的数组特性1:js中的数组可以装任意类型,没有任何限制. // js中的数组特性2: js中的数组,长度是随着下标变化的.用到多长就有多长.
三、date对象(日期)
1.创建date对象
创建date对象 // 方式一: var now = new Date(); console.log(now.toLocaleString()); //2017/9/25 下午6:37:16 console.log(now.toLocaleDateString()); //2017/9/25 // 方式二 var now2 = new Date('2004/2/3 11:12'); console.log(now2.toLocaleString()); //2004/2/3 上午11:12:00 var now3 = new Date('08/02/20 11:12'); //2020/8/2 上午11:12:00 console.log(now3.toLocaleString()); //方法3:参数为毫秒数 var nowd3=new Date(5000); alert(nowd3.toLocaleString( )); alert(nowd3.toUTCString()); //Thu, 01 Jan 1970 00:00:05 GMT
获取日期和时间 getDate() 获取日 getDay () 获取星期 getMonth () 获取月(0-11) getFullYear () 获取完整年份 getYear () 获取年 getHours () 获取小时 getMinutes () 获取分钟 getSeconds () 获取秒 getMilliseconds () 获取毫秒 getTime () 返回累计毫秒数(从1970/1/1午夜)
function foo() { var date = new Date(); var year = date.getFullYear(); var month = date.getMonth(); var day= date.getDate(); var hour = date.getHours(); var min= date.getMinutes(); var week = date.getDay(); console.log(week); var arr=['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; console.log(arr[week]); // console.log(arr[3]); console.log(year+'-'+chengemonth(month+1)+'-'+day+' '+hour+':'+min+':'+arr[week]) } function chengemonth(num) { if (num<10){ return '0'+num } else{ return num } } foo() console.log(foo()) //没有返回值返回undefined //三元运算符 console.log(2>1?2:1)