Javascript 知识与经验(二)—— 基本对象类型

来源:互联网 发布:freertos源码详解 编辑:程序博客网 时间:2024/06/06 10:07

1、基本对象类型 Date:

  Date 类型方法有 UTC,parse 等

console.log("Date.parse('2016/1/3') is " + Date.parse("2016/1/3"));//Date.parse('2016/1/3') is 1451750400000    返回1970年1月1日午夜到指定日期(字符串)的毫秒数console.log("Date.UTC(year,month,day,hours,minutes,seconds,ms) :"+ Date.UTC(2015,8,23));//811814400000 

  Date 类型对象的构造函数如下:

console.log(Date());//特殊且正确,使用了 window.Date 属性,该属性为返回当前时间的日期函数,注意这里的类型是函数而不是对象,是无法调用getDate() 等日期对象拥有的方法的   console.log(Date()) ;//浏览器中正确,原因是js中window对象存在Date属性,该属性为一个返回当前日期字符串的函数,window.Date属性没有日期类型的属性和方法//console.log(Date().getDate()); 将报错console.log(new Date());// 当前时间         console.log(new Date(0));// 1970年1月1日       console.log(new Date(1970,1));//1970年2月1日console.log(new Date(1991,0,0));// 1990年12月31日 返回前一年最后一天console.log(new Date(1991,1,1,90)); //1991-2-4 18:00:00  90小时->3天+18小时console.log(new Date(1991,1,1,0,24*60));// 1991-2-2 24个60分钟进一天,js对Date构造在时分秒部分是从后往前进行叠加时间console.log(new Date(1990,1,1,1,1,1,1,1));// 1991-2-1 01:01:01  

  Date 对象的常用方法:

var date = new Date();console.log(new Date(1971,1,1).constructor());//Sun Sep 04 2016 05:00:13 GMT+0800 (China Standard Time)调用构造器函数总是返回当前时间console.log("getDate()\t\t: " + date.getDate());//4 从 Date 对象返回一个月中的某一天 (1 ~ 31)。console.log("getDay()\t\t: " + date.getDay());  //0 从 Date 对象返回一周中的某一天 (0 ~ 6)console.log("getMonth()\t\t: " + date.getMonth());//8 从 Date 对象返回月份 (0 ~ 11)console.log("getYear()\t\t: " + date.getYear());//116 获取对象年份,有方法存在异常不要使用,要使用 getFullYear()console.log("getFullYear()\t: " + date.getFullYear());//2016 获取对象年份console.log("getHours()\t\t: " + date.getHours()); //5 返回 Date 对象的小时 (0 ~ 23)console.log("getMinutes()\t: " + date.getMinutes());//console.log("getMilliseconds(): " + date.getMilliseconds());    //742console.log("getTime()\t\t :" + date.getTime());//1472936413742console.log("getTimezoneOffset(): " + date.getTimezoneOffset());//-480 返回本地时间与格林威治标准时间 (GMT) 的分钟差 480/60=8 东八区//世界时香港console.log("getUTCDate()\t: " + date.getUTCDate());//3 3日console.log("getUTCDay()\t: " + date.getUTCDay());//6 周六console.log("getUTCMonth()\t\t: " + date.getMonth());//8 9月份,从 Date 对象返回月份 (0 ~ 11)console.log("getUTCFullYear()\t: " + date.getUTCFullYear());//2016console.log("getUTCHours()\t\t: " + date.getUTCHours()); //21console.log("getUTCMilliseconds(): " + date.getUTCMilliseconds());//    742//以上get 时间等函数均有对应set 函数如:setDate(),setDay(),setMonth()等,设置时将时间转换为毫秒数运算console.log("valueOf():\t\t " + date.valueOf());//  1472936413742console.log("toString()\t\t: "+ date.toString());// Sun Sep 04 2016 05:00:13 GMT+0800 (China Standard Time)   console.log("toTimeString()\t\t: " + date.toTimeString());//05:00:13 GMT+0800 (China Standard Time)console.log("toDateString()\t\t: " + date.toDateString());//Sun Sep 04 2016console.log("toGMTString()\t\t: " + date.toGMTString());//Sat, 03 Sep 2016 21:00:13 GMTconsole.log("toLocaleString()\t: " + date.toLocaleString());//2016/9/4 上午5:00:13console.log("toLocaleTimeString(): "+ date.toLocaleTimeString());//上午5:00:13console.log("toLocaleDateString(): " + date.toLocaleDateString());//2016/9/4console.log("valueOf()\t\t: "date.valueOf());// 1472936413742  返回 Date 对象的原始值

2、基本对象类型 Array:

  数组对象的构造或初始化

var emp = new Array();//空数组console.log(emp.length);// 长度为0var arr = new Array(1);//初始化长度为1的数值console.log(arr.length);// 长度为1 console.log(arr[0]);// undefinedvar arr2 = new Array(1,1);//数组 [1,1]console.log(arr2);  console.log(arr3);arr[1] = "对象";console.log(arr);// 输出 [1:"对象"]console.log(arr[2]);// undefinedconsole.log(arr.length);// 长度变为 2console.log(new Array("133"));//console.log(new Array(5,"123"));// [5,5]console.log([]);//使用json 对象表达式初始化一个空数组console.log([,3]);//长度为2的数值 [undefined,3]var arr3 = new Array(undefined,1);// 可通过js检查,创建数组[undefined,1]//new Array(,1);//不可通过js检查

  数组对象的属性:
   length:返回数组的长度
  数组对象的方法:

//contact(arr2.arr3):连接两个或多个数组返回新数组console.log('[1,2,].concat([9,4]) is ');console.log([1,2,].concat([9,4]));//  [1,2,].concat([9,4]) is 1,2,9,4//join(char):使用指定字符将所有数组放入一个字符串,元素通过char字符分隔console.log("[1,2,3,4,null,5,6,undefined,8].join('') is " +[1,2,3,4,null,5,6,undefined,8].join(''));//[1,2,3,4,5,6,undefined,8].join('') is 1234568 null,undefined被排除了//pop():删除并返回数组的最后一个元素console.log("[1,2].pop() is " + [1,2].pop());//[1,2].pop() is 2//push(arr):向数组的末尾添加一个或更多元素,并返回新的长度。var arr = [1,2,];console.log("[1,2,].push([4,5]) return is " + arr.push(4,5));//[1,2,].push([4,5]) return is 4console.log("arr now is ");console.log(arr);//arr now is 1,2,4,5//reverse():颠倒数组中元素的顺序console.log("[1,2].reverse() is ");console.log([1,2].reverse());//[1,2].reverse() is 2,1//sort(func):使用排序函数排序console.log("[1,3,4,2,9].sort(func) is ");console.log([1,3,4,2,9].sort(function(a,b) {    if (a==3) {return 1;}    return a > b;}));//[1,3,4,2,9].sort(func) is 1,2,4,9,3var arr2 = [,1,2];//shift():删除并返回数组的第一个元素var o = arr2.shift();console.log("[,1,2].shift() return is " + arr2.shift())//[,1,2].shift() return is 1console.log("[,1,2] after shift() is ");console.log(arr);//[,1,2] after shift() is [1,2,4,5]//unshift():向数组的开头添加一个或更多元素,并返回新的长度arr = [1,2];var length = arr.unshift(3,5);//[1,2].unshift(3,5) return is 4console.log("[1,2].unshift(3,5) return is " + length);//[1,2] after unshift(3,5) is 3,5,1,2console.log("[1,2] after unshift(3,5) is ");console.log(arr);//[3, 5, 1, 2]//toSring():将数组装换为字符串console.log("[3, 5, 1, 2].toString() return is " +arr.toString());//3,5,1,2//slice(from,to): 获取索引from到to左闭右开区间内的对象console.log([0,1,2,3,4,5,6,7].slice(2,4));//[1,2]    获取索引包括为2,3不包括4等组成的数组//splice(index,num,a,b,...c):删除索引index后的num个元素,并插入若干新元素a,b...c,返回被删除元素组成的数值arr = [1,2,3,4,5];arr2 = arr.splice(0,2,-1,-2);console.log("[1,2,3,4,5].splice(0,2,-1,-2) return is ");console.log(arr2);//[1, 2]console.log("[1,2,3,4,5] after splice(0,2,-1,-2) is ");console.log(arr);//[-1, -2, 3, 4, 5]

3、 基本对象类型 RegExp:

RegExp 对象的构造函数:    函数定义:new RegExp(pattern,attrs)      参数:pattern  正则表达式字符串            attrs   字符i,g和m的一个组合字符串var reg = /{d}5/;//RegEx 字面量reg = new RegEx();//使用构造函数

4、 基本对象类型 Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError:

0 0
原创粉丝点击