1.将字符串‘get-element-by-id’,转换成驼峰表示法‘getElementById’
2.数组去重
//方法三: set
[...new Set(arr)]
3.快速排序
function quickSort(a) { return a.length <= 1 ? a : quickSort(a.slice(1).filter(item => item <= a[0])).concat(a[0], quickSort(a.slice(1).filter(item => item > a[0]))); } console.log(quickSort([12,3,54,43,23,1,5,23,9]))
function quickSort(array){ function sort(prev, numsize){ var nonius = prev; var j = numsize -1; var flag = array[prev]; if ((numsize - prev) > 1) { while(nonius < j){ for(; nonius < j; j--){ if (array[j] < flag) { array[nonius++] = array[j]; break; }; } for( ; nonius < j; nonius++){ if (array[nonius] > flag){ array[j--] = array[nonius]; break; } } } array[nonius] = flag; sort(0, nonius); sort(nonius + 1, numsize); } } sort(0, array.length); return array; } quickSort(arr)
4.将类数组转化为真数组
//方法一 var obj = {0: 'a', 1: 'b', length: 2} var arr = Array.prototype.slice.call(obj) //实现原理 Array.prototype.slice = function(start,end){ var result = new Array(); start = start || 0; end = end || this.length; //this指向调用的对象,当用了call后,能够改变this的指向,也就是指向传进来的对象 for(var i = start; i < end; i++){ result.push(this[i]); } return result; } //方法二: Array.from()
5. 输出的结果
var a = [1, 2, 3, 4]
i = 0
a[i++] = a[i++] * 2
console.log(a) //[4, 2, 3, 4]
6. b(); a() //bbb, a is not a function
var a = function() {
console.log('aaa')
}
function b() {
console.log('bbb')
}
7. var name = 'global'
var obj = {
name: 'obj',
dose : function(){
this.name = 'dose'
return function(){
return this.name
}
}
}
var result = obj.dose().call(this) //global