JS常见对象方法小结

来源:互联网 发布:淘宝设置粉丝圈 编辑:程序博客网 时间:2024/04/29 19:30

Array对象

  方法:

concat()——连接两个或更多数组,返回结果

arrayObject.concat(arrayX,arrayX,......,arrayX)

join()——把所有元素放入一个字符串,可传入分隔符,返回字符串
arr.join(".")

push()——向数组的末尾添加元素,返回新的长度

arrayObject.push(newelement1,newelement2,....,newelementX)

pop()——删除数组最后一个元素,返回删除的这个元素

arrayObject.pop()

splice()——向数组中添加或删除项目,参数为添加或删除的位置、删除的数量、添加的项目,返回删除的项目

arrayObject.splice(index,howmany,item1,.....,itemX)

sort()——把数组排序,参数为用于规定排序顺序的函数

arrayObject.sort(function(a, b){return a - b;});  //升序  b - a降序


slice()——从已有数组中选定返回的元素,返回选定的元素

arrayObject.slice(start,end)

shift()——把数组第一个元素返回,返回第一个元素

arrayObject.shift()

reverse()——颠倒数组中元素的顺序,直接修改原来的数组

arrayObject.reverse()

toSource()——返回对象的源代码

object.toSource()

toString()——把数组转换成字符串,返回结果

arrayObject.toString()

unshift()——向数组的开头添加一个或多个对象,返回新的长度

arrayObject.unshift(newelement1,newelement2,....,newelementX)

valueOf()——用于返回数组的原始对象

arrayObject.valueOf()

Date对象

  方法:

date()——返回当天的日期和时间

Fri Sep 02 2016 21:21:39 GMT+0800   //输出结果

getDate()——返回月份的某一天

dateObject.getDate()    //输出结果例:2

getDay()——返回星期中某一天的数字

var d=new Date()var weekday=new Array(7)weekday[0]="Sunday"weekday[1]="Monday"weekday[2]="Tuesday"weekday[3]="Wednesday"weekday[4]="Thursday"weekday[5]="Friday"weekday[6]="Saturday"document.write("Today it is " + weekday[d.getDay()])

getMonth()——返回表示月份的数字

<script type="text/javascript">var d=new Date()var month=new Array(12)month[0]="January"month[1]="February"month[2]="March"month[3]="April"month[4]="May"month[5]="June"month[6]="July"month[7]="August"month[8]="September"month[9]="October"month[10]="November"month[11]="December"document.write("The month is " + month[d.getMonth()])</script>

getYear()——返回表示年份的两位或四位的数字,1900至1999返回两位数字、1900之前或1999之后的返回四位数字

dateObject.getYear()

getHours()——返回时间的小时字段

dateObject.getHours()

getMinutes() ——方法可返回时间的分钟字段

dateObject.getMinutes()

getSeconds()——方法可返回时间的秒

dateObject.getSeconds()

getTime()——返回距1970年1月1日的毫秒数

dateObject.getTime()

setTime()——方法以毫秒设置 Date 对象

dateObject.setTime(millisec)

parse()——方法可解析一个日期时间字符串,并返回 1970/1/1 午夜距离该日期时间的毫秒数

Date.parse(datestring)

Math对象

  方法:

abs(x)——返回数的绝对值


ceil(x)——把数向上舍入


floor(x)——把数向下舍入


max(x, y)——返回最大值


min(x, y)——返回最小值


pow(x, y)——返回x的y次幂


random()——返回0-1间的随机数


round(x)——把数四舍五入为最接近的整数


sqrt(x)——返回数的平方根


Number对象

  方法:

toFixed()——把Number 四舍五入为指定小数位数的数字

NumberObject.toFixed(num)

toExponential()——方法可把对象的值转换成指数计数法

NumberObject.toExponential(num)

toPrecision()——方法可在对象的值超出指定位数时将其转换为指数计数法

NumberObject.toPrecision(num)

String对象

  方法:

big()——方法用于把字符串显示为大号字体

stringObject.big()

blink()——方法用于显示闪动的字符串

stringObject.blink()

bold()——方法用于把字符串显示为粗体

stringObject.bold()

charAt()——方法可返回指定位置的字符

stringObject.charAt(index)

concat()——方法用于连接两个或多个字符串

stringObject.concat(stringX,stringX,...,stringX)

fixed()——方法用于把字符串显示为打字机字体

stringObject.fixed()

indexOf()——方法可返回某个指定的字符串值在字符串中首次出现的位置

stringObject.indexOf(searchvalue,fromindex)

match()——方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配

stringObject.match(searchvalue)  //字符串值stringObject.match(regexp)       //规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。


replace()——方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串

stringObject.replace(regexp/substr,replacement)


slice()——方法可提取字符串的某个部分,并以新的字符串返回被提取的部分

stringObject.slice(start,end)


split()——方法用于把一个字符串分割成字符串数组

stringObject.split(separator,howmany)


strike()-方法用于显示加删除线的字符串

stringObject.strike()


substr()——方法可在字符串中抽取从 start 下标开始的指定数目的字符

stringObject.substr(start,length)


substring()——方法用于提取字符串中介于两个指定下标之间的字符

stringObject.substring(start,stop)


Window对象

  方法:

alert()——方法用于显示带有一条指定消息和一个 OK 按钮的警告框

alert(message)


blur()——方法可把键盘焦点从顶层窗口移开

window.blur()


close()——方法用于关闭浏览器窗口

window.close()


confirm()——方法用于显示一个带有指定消息和 OK 及取消按钮的对话框

confirm(message)

moveBy()——方法可相对窗口的当前坐标把它移动指定的像素

window.moveBy(x,y)


moveTo()——方法可把窗口的左上角移动到一个指定的坐标

window.moveTo(x,y)


open()——方法用于打开一个新的浏览器窗口或查找一个已命名的窗口

window.open(URL,name,features,replace)


prompt()——方法用于显示可提示用户进行输入的对话框

prompt(text,defaultText)


resizeBy()——方法用于根据指定的像素来调整窗口的大小

resizeBy(width,height)


resizeTo()——方法用于把窗口大小调整为指定的宽度和高度

resizeTo(width,height)


scrollBy()——方法可把内容滚动指定的像素数

scrollBy(xnum,ynum)


scrollTo()——方法可把内容滚动到指定的坐标

scrollTo(xpos,ypos)


setInterval()——方法可按照指定的周期(以毫秒计)来调用函数或计算表达式

setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数

setInterval(code,millisec[,"lang"])


clearInterval()——方法可取消由 setInterval() 设置的 timeout

clearInterval() 方法的参数必须是由 setInterval() 返回的 ID 值

clearInterval(id_of_setinterval)


setTimeout()——方法用于在指定的毫秒数后调用函数或计算表达式

setTimeout(code,millisec)


clearTimeout()——方法可取消由 setTimeout() 方法设置的 timeout

clearTimeout(id_of_settimeout)



0 0