JavaScript中的内置对象

javascript中的内置对象有:

1.Array

2.String

3.Math

4.Data


一.JavaScript中的数组(array):

创建数组的基本方式有两种:

1.使用Array构造函数new Array()

2.使用数组字面量表示法

var str=[1,2,3,4];


数组长度:

array.length

返回值:number


数组的栈方法:

1.push()

2.unshift()

3.pop()

4.shift()


push():

arrayObject.push(newle1,newle2,....,newlen)

把它的参数顺序添加到arrayObject的尾部

返回值:把指定的值添加到数组后的新长度。


unshift():

arrayObject.unshift(newle1,newle2,....,newlen)

把它的参数顺序添加到arrayObject的开头

返回值:把指定的值添加到数组后的新长度。


pop():

arrayObject.pop()

删除arrayObject的最后一个元素

返回值:被删除的那个元素。


shift():

arrayObject.shift()

删除arrayObject的第一个元素

返回值:被删除的那个元素。


join():
arrayObject.join(separator)

用于把数组中的所有元素放入一个数组。

返回值:字符串


reverse(转换方法):

arrayObject.reverse()

用于颠倒数组中元素的顺序

返回值:数组


sort():(数组的重排序方法)

arrayObject.sort(sortby)

用于对数组的元素进行排序

返回值:数组

sort()方法比较的是字符串。即使数组中的每一项都是数值。可以接收一个比较函数作为参数

var arr=[9,23,15,-99,88,12,-2];
	   /*降序 return 参数1<参数2
	   arr.sort(function(a,b){return a<b});
	   升序 return 参数1>参数2*/
	   arr.sort(function(a,b){return a>b});
	   console.log(arr);


数组的操作方法:

1.concat()

2.slice()


concat():

arrayObject.concat(array1,array2,array3,....,arrayn)

用于连接两个或多个数组。

返回值:数组


slice():

arrayObject(start,end)

从已有的数组中返回选定的元素

参数:start(必须要有)end(可选)

返回值:数组


splice():(对数组项进行删除 插入 替换操作)

①arrayObject.splice(index,count)

删除从index处开始的零个或多个元素

返回值:被删除的数组。

注意:count若为0,则不会删除项目。若不设置,则删除从index开始的所有值。

②arrayObject.splice(index,0,item1,item2,....,itemn)

在指定位置插入

返回值:数组

③arrayObject.splice(index,count,item1,item2,....,itemn)

在指定位置插入值,且同时删除任意数量的项

返回值:从原始数组中删除的项(如果没有删除任何项,则返回空数组)


indexOf():(为数组实例添加的位置方法):

arrayObject.indexOf(searchvalue,startIndex)

从数组的开头(位置0)开始向后查找。

参数:searchvalue(必须要有),要查找的项。

    startIndex(可选),起点位置的索引。

返回值:number


lastIndexOf():(为数组实例添加的位置方法):

arrayObject.lastIndexOf(searchvalue,startIndex)

从数组的末尾开始向前查找。

参数:searchvalue(必须要有),要查找的项。

    startIndex(可选),起点位置的索引。

返回值:number

注意:所查找的项除了值相等以外还要数据类型相同,若要自己封装方法需使用===。

点击打开链接


二.JavaScript中的string:

charAt():

stringObject.charAt(index)

返回stringObject中的index位置的字符。


charCodeAt():

stringObject.charCodeAt(index)

返回stringObject中的index位置的字符的字符编码。


indexOf():

stringObject.indexOf("o")

从一个字符串中搜索给定的子字符串,返回子字符串的位置。

返回值:数值

若果没有找到子字符串则返回-1


lastIndexOf():

stringObject.lastIndexOf("o")

从一个字符串中搜索给定的子字符串,返回子字符串的位置。

返回值:数值

若果没有找到子字符串则返回-1


截取方法:

slice()

substring()

substr()


slice():

stringValue.slice(start,end)

截取子字符串。

参数:start(必须要有)指定子字符串开始位置

    end(可选)表示子字符串到哪里结束,end本身不在截取范围之内,省略时截取至字符串的末尾。


substring():

语法及功能和slice()完全一样。

区别在于:

当参数为负数时,自动将参数转换为0.

substring()会将较小的数作为开始位置,将较大的数作为结束位置。


substr():

stringValue.substr(start,len)

截取子字符串

参数:start(必须要有)

当len为负值时返回空字符串。

//获取扩展名
	var url="http://baidu.com/index.text";
	function getFileFormat(url){
		var pos=url.lastIndexOf(".");
		return url.substr(pos);
	}
	var formatName=getFileFormat(url);
	var twoFormat=getFileFormat("zerobaek.jpg");
	console.log(formatName);
	console.log(twoFormat);


split():

stringObject.split(separator)

把一个字符串分割成字符串数组。

返回值:Array

separator(必须要有),分隔符


replace():

stringObject.repalce(regexp/substr,replacement)

在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的字符串

返回值:string

regexp/substr(必须要有)规定子字符串或要替换的模式的RegExp对象

replacement(必须要有)一个字符串值


toUpperCase():

stringValue.toUpperCase()

把字符串转换为大写。


toLowerCase():

stringValue.toLowerCase()

把字符串转换为小写。


实例:将类似于border-left-color转换成borderLeftColor

//驼峰形式
	function camelback(str){
		var arr=str.split("-"),
			newStr=arr[0];
		for(var i=1,len=arr.length;i<len;i++){
			var word=arr[i];
			newStr+=word.charAt(0).toUpperCase()+word.substr(1);
		}
		return newStr;
	}
	var camelFormat=camelback("zero-baek-by");
	console.log(camelFormat);


三.JavaScript中的Math:

min()

max()

ceil()

floor()

round()

abs()


Math.min(num1,num2,...,numN)

求一组数中的最小值

返回值:Number


Math.max(num1,num2,...,numN)

求一组数中的最大值

返回值:Number


Math.ceil(num)

向上取整,即返回大于num的最小整数

返回值:Number


Math.floor(num)

向下取整,返回num的整数部分

返回值:Number


Math.round(num)

将数值四舍五入为最接近的整数

返回值:Number


Math.abs(num)

返回num的绝对值

返回值:Number


Math.random()

返回大于等于0小于1的一个随机数

返回值:Number

注意:

求n到m之间的随机整数的公式

random=Math.floor(Math.random()*(m-n+1)+n)

// 生成一个n到m之间的随机整数
       function getRandom(n,m){
          var choise=m-n+1;  // 随机整数的个数
          return Math.floor(Math.random()*choise+n);
       }
       var random1=getRandom(2,6);
       var random2=getRandom(10,88);

四.JavaScript中的data对象

new Data():

创建一个日期时间对象

不传参的情况下,返回当前的日期时间对象。

如果想根据特定的日期和时间创建日期对象,必须穿入表示该日期的毫秒数或者一组用逗号隔开的表示年月日时分秒的参数。


获取年月日时分秒及星期的方法

getFullYear():返回四位数的年份

getMonth():返回日期中的月份,返回值为0-11

getDate():返回月份中的天数

getDay():返回星期,返回值为0-6

getHours():返回小时

getMinutes():返回分

getSeconds():返回秒

getTime():返回表示日期的毫秒数

           // 创建一个日期时间对象
       var weeks=["日","一","二","三","四","五","六"],
           today=new Date(),
           year=today.getFullYear(),
           month=today.getMonth()+1,
           date=today.getDate(),
           week=today.getDay(),
           hours=today.getHours(),
           minutes=today.getMinutes(),
           seconds=today.getSeconds(),
           times=today.getTime(),
           time=year+'年'+month+'月'+date+'日'+hours+'时'+minutes+'分'+seconds+'秒 星期'+weeks[week];
       console.log("现在是:"+time);
       console.log(times);

设置年月日时分秒及星期的方法

setFullYear():设置四位数的年份

setMonth():设置日期中的月份,设置值为0-11(setMonth(month,day),其中month参数必须要有,day参数可有可无)

setDate():设置月份中的天数

setDay():设置星期,设置值为0-6

setHours():设置小时

setMinutes():设置分

setSeconds():设置秒

setTime():以毫秒数设置日期,会改变整个日期

	function addZero(num){
           if(num<10){
              return '0'+num;
           }else{
              return num;
           }
        }
        // 返回n天之后的日期时间对象
        function get_date(n){
           // 判断n,如果是未定义的,则返回当前日期,否则返回n天之后的日期
           n=typeof(n)==="undefined"?0:n;
           // 创建一个当前的日期时间对象
           var date=new Date(),
               times=date.getTime(),   // 到现在为止的毫秒数
               tempDate=new Date(),    // 未来的一个日期对象
               //times=date*1;  // 等价于getTime()隐式类型转换
               tempTimes=times+86400000*n,
               year,mon,day;
           // 将tempTimes设置为当前
           tempDate.setTime(tempTimes);
           year=tempDate.getFullYear();
           mon=addZero(tempDate.getMonth()+1);
           day=addZero(tempDate.getDate());
           return year+'-'+mon+'-'+day;
        }
        console.log(get_date(20));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值