Object.create(null); 创建空 json
正则 replace
//function(匹配到的元素,替换后的元素,被匹配到的index,str)
str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; })
Object.keys(a) 要返回其枚举自身属性的对象
Object.values()和Object.keys()是相反的操作,把一个对象的值转换为数组
// 一、处理对象,返回可枚举的属性数组
let person2 = {name:"张三",age:25,address:"深圳",getName:function(){}}
console.log(Object.keys(person2)); // ["name", "age", "address", "getName"]
//常用技巧
Object.keys(person2).map( (key)=>{ return person[key] } ) //["张三", 25, "深圳", ƒ]
//二、处理数组,返回索引值
let arr1 = ['a', 'b', 'c'];
console.log(Object.keys(arr1)); // ["0", "1", "2"]
//三、处理字符串,返回索引值数组
let str1 = '123abc字符串';
console.log(Object.keys(str1)); //["0", "1", "2", "3", "4", "5", "6", "7", "8"]
Array.every
var aa = [{"111":"aa"},{"222":"bbbb"}];
aa.every(function(match, index, aaArray){console.log(match);console.log(index);console.log(aaArray);})
bind实现
/* istanbul ignore next */
function polyfillBind (fn, ctx) {
function boundFn (a) {
var l = arguments.length;
return l
? l > 1
? fn.apply(ctx, arguments)
: fn.call(ctx, a)
: fn.call(ctx)
}
boundFn._length = fn.length;
return boundFn
}
function nativeBind (fn, ctx) {
return fn.bind(ctx)
}
var bind = Function.prototype.bind
? nativeBind
: polyfillBind;
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
reduce() 可以作为一个高阶函数,用于函数的 compose。
注意: reduce() 对于空数组是不会执行回调函数的。
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
绑定一次事件
function once (fn) {
var called = false;
return function () {
if (!called) {
called = true;
fn.apply(this, arguments);
}
}
}