[笔记] 面向现代 JavaScript 转型

Morden Browser:

  1. Chrome
  2. Firefox
  3. Safari
  4. Edge

Features:

  1. Classes 95.5%
class Profile extends Widget {
	constructor() {
		super();
	}
	static load() {
		return apiCall('/profile');
	}
}
  1. Arrow functions 96%
const add = (a, b) => a + b
class Profile {
	update() {
		Profile.load().then(data => {
			this.render(data);
		});
	}
}
  1. Generators 96%
function* search(node) {
	if (!node) return;
	yield node;
	yield* search(node.firstChild);
	yield* search(node.nextSibling);
}

for (let node of search(document)) {
	if (node.localName === 'title') {
		console.log(node.textContent);
		break;
	}
}
  1. Block Scoping 95%
const events = {
	click: log,
	mouseup: log
}

for ( let type in events ) {
	let fn = events[type];
	addEventListener(type, function(e) {
		fn(e);
	});
}
  1. Destructuring 94%
function add({a, b=0}) {
	return a + b;
}

const { x, y, z=0 } = { x:1, y:2 };

const [ a, ...rest ] = 'abc'.split('');
  1. Rest & Spread 94%
function h(a, b, ...c) { }

Math.max(...[1,2,3]);

let unique = [...new Set([1, 2, 2, 3])];

  1. Object Shorthand 95%
let a = 1, b = 2;
const obj = {a, b};

const Myth = {
	random() { return 42; }
}
  1. Async / Await 95%
class Profile {
	async update() {
		this.render(await Profile.load());
	}
}

for (const a of document.links) {
	const res = await fetch(a.href);
	if (!res.ok) a.classList.add('gone');
}

PS : 一些 WebAssembly 相关的资料

赞赏