清点JavaScript里好用的原生API ꒰・◡・๑꒱

这段时刻翻了一番JavaScript的api,发明不少好的轮子,省去造的麻烦了。

直接进入正题

剖析字符串对象

我们都晓得,JavaScript对象能够序列化为JSON,JSON也能够剖析成对象,然则问题是假如涌现了一个既不是JSON也不是对象的"东西",转成哪一方都不轻易,那末eval就能够派上用场

var obj = "{a:1,b:2}";   // 看起来像对象的字符串
eval("("+ obj +")")      // {a: 1, b: 2}

由于 eval 能够实行字符串表达式,我们愿望将 obj 这个字符串对象 实行成真正的对象,那末就须要用eval。然则为了防止eval 将带 {} 的 obj 当语句来实行,我们就在obj的表面套了对 (),让其被剖析成表达式。

& (按位与)

推断一个数是不是为2的n次幂,能够将其与本身减一相与

var number = 4
(number & number -1) === 0 // true

^ (按位异或)

不必第三个变量,就能够交流两个变量的值
var a = 4,b = 3
a = a ^ b  //    7
b = a ^ b  //    4
a = a ^ b  //    3

格式化Date

想获得format后的时刻?如今不必再get年月日时分秒了,三步搞定

var temp = new Date();
var regex = /\//g;
(temp.toLocaleDateString() + ' ' + temp.toLocaleTimeString().slice(2)).replace(regex,'-');

// "2015-5-7 9:04:10"

想将format后的时刻转换为时刻对象?直接用Date的组织函数

new Date("2015-5-7 9:04:10");

// Thu May 07 2015 09:04:10 GMT+0800 (CST)
经测试发明火狐没法对format后的时刻字符串运用Date.parse(),故这个要领在火狐上不好使

想将一个规范的时刻对象转换为unix时刻戳?valueOf搞定之

(new Date).valueOf();

// 1431004132641

很多朋侪还提醒了如许能够疾速获得时刻戳

+new Date
// 1431004132641

一元加

一元加能够疾速将字符串的数字转换为数学数字,即
var number = "23" 
typeof number  // string
typeof +number // number

能够将时刻对象转为时刻戳
new Date  //  Tue May 12 2015 22:21:33 GMT+0800 (CST)
+new Date //  1431440459887

转义URI

须要将url当作参数在路由中通报,如今转义之

var url = 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是运用最频仍的范例检测手腕

typeof 3        // "number"
typeof "333"    // "string"
typeof false    // "boolean"

关于基础(简朴)数据范例照样挺好的,然则一旦到了援用数据范例的时刻,就不那末好使了

typeof new Date()   // "object"
typeof []           // "object"
typeof {}           // "object"
typeof null         // "object"      

前三个还能忍,null竟然也返回object,你是在逗我吗!!!(ps:实在这是JavaScript的bug 人艰不拆 ꒰・◡・๑꒱ )

这时候,我们会运用instanceof

toString instanceof Function
// true
(new Date) instanceof Date
// true
[] instanceof Object
// true
[] instanceof Array
// true

实在我们能够发明,[] 和 Object获得了true,虽然我们晓得,[]也是对象,然则我们愿望一个能更正确的推断范例的要领,如今它来了
运用Object.prototype.toString()来推断,为了让每个对象都能经由过程检测,我们须要运用Function.prototype.call或许Function.prototype.apply的情势来挪用

var toString = Object.prototype.toString;

toString.call(new Date)    // "[object Date]"
toString.call(new Array)   // "[object Array]"
toString.call(new Object)  // "[object Object]"
toString.call(new Number)  // "[object Number]"
toString.call(new String)  // "[object String]"
toString.call(new Boolean) // "[object Boolean]"

要注重的是:toString要领极有能够被重写,所以须要运用的时刻,
能够直接运用Object.prototype.toString()要领

完成继续

看一个官方给的例子
//Shape - superclass
function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); //call super constructor.
}

Rectangle.prototype = Object.create(Shape.prototype);

var rect = new Rectangle();

rect instanceof Rectangle //true.
rect instanceof Shape //true.

rect.move(1, 1); //Outputs, "Shape moved."

经由过程call来猎取初始化的属性和要领,经由过程Object.create来猎取原型对象上的属性和要领

迭代

ES5出了挺多的迭代函数,如map,filter,some,every,reduce等,这里有传送门,能够看到挺多的例子

http://segmentfault.com/a/1190000002687651

Array

细致的api这里引见的很细致。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Glob…

这里就提几句:
pop,push,reverse,shift,sort,splice,unshift会转变原数组

join,concat,indexOf,lastIndexOf,slice,toString不会转变原数组

map,filter,some,every,reduce,forEach这些迭代要领不会转变原数组

几个注重点:

1 shift,pop会返回谁人被删除的元素
2 splice 会返回被删除元素构成的数组,或许为空数组
3 push 会返回新数组长度
4 some 在有true的时刻住手
5 every 在有false的时刻住手
6 上述的迭代要领能够在末了追加一个参数thisArg,它是实行 callback 时的 this 值。
    原文作者:qianjiahao
    原文地址: https://segmentfault.com/a/1190000002753931
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞