ynchuan / blog-ynchuan Goto Github PK
View Code? Open in Web Editor NEWrewrite blog base on react and so on
rewrite blog base on react and so on
var obj = {
a: 'obja',
b: 'objb',
c: 'objc',
d: 'objd',
};
var arr = ['arr1', 'arr2', 'arr3', 'arr4'];
for (var key in obj) {
console.log(key);
console.log("====" + obj[key]);
}
for (var key in arr) {
console.log(key);
console.log("====" + arr[key]);
}
for (var key of arr) {
console.log(key);
}
for (var key of obj) { //报错,没有继承iterator
console.log(key);
}
利用事件冒泡实现类似$('dom').on('click','.name,.age',function(e){})调用的函数;
/**
* 函数节流(throttle):某个动作规定的时间内只执行第一次
*/
const throttle = (action, idle) => {
let last = 0;
return (...prop) => {
let curr = +new Date;
if (curr - last > idle) {
action.apply(this, prop);
last = curr;
}
}
};
const throttleAction = throttle((...prop) => {
console.log(prop);
}, 1000);
throttleAction('throttle1');
throttleAction('throttle2');
throttleAction('throttle3'); // 输出 throttle1
setTimeout(() => {
throttleAction('throttle4');
throttleAction('throttle5');
throttleAction('throttle6'); // 输出 throttle4
}, 2000);
/**
* 函数防抖 (debounce):某个动作规定的时间内只执行最后一次
*/
const debounce = (action, idle) => {
let handler;
return (...prop) => {
handler && clearTimeout(handler);
handler = setTimeout(() => {
action.apply(this, prop);
}, idle);
}
};
const debounceAction = debounce((...prop) => {
console.log(prop);
}, 1000);
debounceAction('debounce1');
debounceAction('debounce2');
debounceAction('debounce3'); // 输出 debounce3
setTimeout(() => {
debounceAction('debounce4');
debounceAction('debounce5');
debounceAction('debounce6'); // 输出 debounce6
}, 2000);
/**
* 由于map具有有序性和一一映射的特性,所以map可以转换为数组和对象
* map->array 二维数组形式,可逆
* map->object 键值对形式,可逆
*/
let mapToObj = (map) => {
let obj = Object.create(null);
for ([key, value] of map.entries()) {
obj[key] = value;
}
return obj;
}
let map = new Map();
map.set('a', 'a').set('c', 1).set({}, {}).set([1, 2], [3, 4]);
console.log("map to object test:");
console.log(mapToObj(map));
let objToMap = (obj) => {
let ret = new Map;
for (let key in obj) {
ret.set(key, obj[key]);
}
return ret;
};
let obj = { 'a': 1, '2': 'b' };
console.log("object to map test:");
console.log(objToMap(obj));
let arrToMap = (arr) => {
return new Map(arr);
};
let arr = [
[1, 'a'],
[2, 'b'],
[4, 'd']
];
console.log("array to Map test:");
console.log(arrToMap(arr));
let mapToArr = (map) => {
return [...map];
};
console.log("map to array test:");
console.log(mapToArr(new Map().set('a', 'a').set('c', 1).set({}, {}).set([1, 2], [3, 4])));
let jsonToMap1 = (json) => {
return objToMap(JSON.parse(json));
};
let jsonToMap2 = (json) => {
return new Map(JSON.parse(json));
};
let json1 = '{"a":"a","b":"b"}';
console.log("json to map test 1:");
console.log(jsonToMap1(json1));
let json2 = '[["a","1"],["b"],[3,"c"]]';
console.log("json to map test 2:");
console.log(jsonToMap2(json2));
let mapToJson = (map) => {
return JSON.stringify(mapToObj(map));
};
console.log("map to array json:");
console.log(mapToJson(new Map().set('a', 'a').set('c', 1).set({}, {}).set([1, 2], [3, 4])));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.