JavaScript里实用的原生API汇总
直接进入正题
解析字符串对象
我们都知道,JavaScript对象可以序列化为JSON,JSON也可以解析成对象,但是问题是如果出现了一个既不是JSON也不是对象的"东西",转成哪一方都不方便,那么eval就可以派上用场
varobj="{a:1,b:2}";//看起来像对象的字符串 eval("("+obj+")")//{a:1,b:2}
因为eval可以执行字符串表达式,我们希望将obj这个字符串对象执行成真正的对象,那么就需要用eval。但是为了避免eval将带{}的obj当语句来执行,我们就在obj的外面套了对(),让其被解析成表达式。
&(按位与)
判断一个数是否为2的n次幂,可以将其与自身减一相与
varnumber=4 (number&number-1)===0//true
^(按位异或)
不同第三个变量,就可以交换两个变量的值
vara=4,b=3 a=a^b//7 b=a^b//4 a=b^a//3
格式化Date
想得到format后的时间?现在不用再get年月日时分秒了,三步搞定
vartemp=newDate(); varregex=/\//g; (temp.toLocaleDateString()+''+temp.toLocaleTimeString().slice(2)).replace(regex,'-'); //"2015-5-79:04:10"
想将format后的时间转换为时间对象?直接用Date的构造函数
newDate("2015-5-79:04:10"); //ThuMay07201509:04:10GMT+0800(CST)
想将一个标准的时间对象转换为unix时间戳?valueOf搞定之
(newDate).valueOf(); //1431004132641
许多朋友还提醒了这样可以快速得到时间戳
+newDate
一元加
一元加可以快速将字符串的数字转换为数学数字,即
varnumber="23" typeofnumber//string typeof+number//number
可以将时间对象转为时间戳
newDate//TueMay12201522:21:33GMT+0800(CST) +newDate//1431440459887
转义URI
需要将url当做参数在路由中传递,现在转义之
varurl=encodeURIComponent('http://segmentfault.com/questions/newest') //"http%3A%2F%2Fsegmentfault.com%2Fquestions%2Fnewest"
再反转义
decodeURIComponent(url) //"http://segmentfault.com/questions/newest"
Number
希望保留小数点后的几位小数,不用再做字符串截取了,toFixed拿走
number.toFixed()//"12346" number.toFixed(3)//"12345.679" number.toFixed(6)//"12345.678900"
参数范围为0~20,不写默认0
类型检测
typeof是使用最频繁的类型检测手段
typeof3//"number" typeof"333"//"string" typeoffalse//"boolean"
对于基本(简单)数据类型还是挺好的,但是一旦到了引用数据类型的时候,就不那么好使了
typeofnewDate()//"object" typeof[]//"object" typeof{}//"object" typeofnull//"object"
前三个还能忍,null居然也返回object,你是在逗我吗!!!(ps:其实这是JavaScript的bug人艰不拆꒰・◡・๑꒱)
这时,我们会使用instanceof
toStringinstanceofFunction //true (newDate)instanceofDate //true []instanceofObject //true []instanceofArray //true
其实我们可以发现,[]和Object得到了true,虽然我们知道,[]也是对象,但是我们希望一个能更准确的判断类型的方法,现在它来了
使用Object.prototype.toString()来判断,为了让每一个对象都能通过检测,我们需要使用Function.prototype.call或者Function.prototype.apply的形式来调用
vartoString=Object.prototype.toString; toString.call(newDate)//"[objectDate]" toString.call(newArray)//"[objectArray]" toString.call(newObject)//"[objectObject]" toString.call(newNumber)//"[objectNumber]" toString.call(newString)//"[objectString]" toString.call(newBoolean)//"[objectBoolean]"
要注意的是:toString方法极有可能被重写,所以需要使用的时候,
可以直接使用Object.prototype.toString()方法
实现继承
看一个官方给的例子
//Shape-superclass functionShape(){ this.x=0; this.y=0; } Shape.prototype.move=function(x,y){ this.x+=x; this.y+=y; console.info("Shapemoved."); }; //Rectangle-subclass functionRectangle(){ Shape.call(this);//callsuperconstructor. } Rectangle.prototype=Object.create(Shape.prototype); varrect=newRectangle(); rectinstanceofRectangle//true. rectinstanceofShape//true. rect.move(1,1);//Outputs,"Shapemoved."
通过call来获取初始化的属性和方法,通过Object.create来获取原型对象上的属性和方法
迭代
ES5出了挺多的迭代函数,如map,filter,some,every,reduce等
Array
具体的api这里介绍的很详细。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Glob...
这里就提几句:
join,pop,push,reverse,shift,sort,splice,unshift会改变原数组
concat,indexOf,lastIndexOf,slice,toString不会改变原数组
map,filter,some,every,reduce,forEach这些迭代方法不会改变原数组
几个注意点:
1shift,pop会返回那个被删除的元素
2splice会返回被删除元素组成的数组,或者为空数组
3push会返回新数组长度
4some在有true的时候停止
5every在有false的时候停止
6上述的迭代方法可以在最后追加一个参数thisArg,它是执行callback时的this值。
以上所述就是本文的全部内容了,希望大家能够喜欢。