10 进制和 2 进制互相转换

1
2
3
// 浮点数不行
var toTwoNumber=(35).toString(2)
var toTenNumber=parseInt(twoNumber,2)

arguments数组转为参数

1
2
3
4
5
6
7
function a(a1,a2){
console.log(a1,a2)
}
function b(){
a.apply(null,arguments)
}
b(1,2,3,4,5,6) // 1 2

合并 JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var a={a:{b:{c:1}},b:2,c:3},b={a:{e:2},b:{}};
var util={
type:{
Object:'[object Object]',
Undefined:'[object Undefined]'
},
judge:function(d,t){return Object.prototype.toString.call(d)===this.type[t]},
isUndefined:function(d){return this.judge(d,'Undefined')},
isObject:function(d){return this.judge(d,'Object')},
isArray:function(d){return Array.isArray(d)},
mergeJson:function(oa,ob){
if(!util.isObject(oa)||!util.isObject(ob)){
return ob
}
for(var i in oa){
// ob[i] 不存在,或者为 undefined,则用 oa[i] 替换
if(util.isUndefined(ob[i])){
ob[i]=oa[i]
}
// 两个都为对象继续迭代
if(util.isObject(oa[i]) && util.isObject(ob[i])){
util.mergeJson(oa[i],ob[i])
}
if(util.isArray(oa[i])&&util.isArray(ob[i])){
ob[i].concat(oa[i])
}
}
return ob;
}
}
const merge=util.mergeJson;
const result=merge(a,b)