rabbit-tian / rabbit-tian.github.io Goto Github PK
View Code? Open in Web Editor NEW我的博客
我的博客
setTimeout(function () {
console.log('我是定时器');
});
new Promise(function (resolve) {
console.log('马上要执行for循环啦');
for (let i = 0; i < 1000; i++) {
if (i == 999) {
resolve();
}
}
}).then (function () {
console.log('执行then函数');
})
console.log('我是普通的log');
// 执行结果
// 马上要执行for循环啦
// 我是普通的log
// 执行then函数
// 我是定时器
JavaScript本身是一门单线程语言
同步执行和异步执行
forEach
forEach()不会返回有意义的值的。我们在回调函数中直接修改arr的值。
let arr = [1,2,3,4];
arr.forEach(function (e,i) {
arr[i] = e*2;
})
console.log(arr); // [2,4,6,8]
map
map()会返回一个全新的数组,原本的数组不受到影响
let arr2 = [1, 2, 3, 4];
let newArr = arr2.map(function (e) {
return e*2
})
console.log(newArr); // [2,4,6,8]
console.log(arr2); // [1,2,3,4]
forEach适合于你并不打算改变数据的时候,而只是想用数据做一些事情 – 比如存入数据库或则打印出来。
let arr3 = ['a', 'b', 'c', 'd'];
arr3.forEach((letter) => {
console.log(letter);
});
map()适用于你要改变数据值的时候。不仅仅在于它更快,而且返回一个新的数组。这样的优点在于你可以使用复合(composition)(map(), filter(), reduce()等组合使用)来玩出更多的花样。
// 先使用map将每一个元素乘以2,然后紧接着筛选出那些大于5的元素。最终结果赋值给arr2
let arr = [1, 2, 3, 4, 5];
let arr2 = arr.map(num => num * 2).filter(num => num > 5);
// arr2 = [6, 8, 10]
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.