JavaScript 语言核心
“所以在设计产品和编程语言时,我们希望直接使用核心的精华部分,是因为这些精华创造了大部分价值”
学习参考资料:
- JavaScript Definitive GuideBook 6th Edition
- JavaScript Good Part
- JavaScript Reintroduction
- MDN JavaScript Guide
- Official Document with PDF Possible
- Github Understanding ES6
- git.io/es6features
- Babel Version of ES6
基本结构
基础概念
- 严格上来说:脚本中应该启动
"use strict"严格模式 - UNICODE UTF-16 支持
normalize()for more appropriate string
脚本的加载
- 异步加载:
defer以及async实现:异步、延迟加载的意义。延迟脚本会在 DOM 解析完毕之后再执行。- 注意:现实中,延迟脚本并不一定按照顺序加载,也不一定会在
DOMContentLoaded事件触发前执行,因此比较理想的是只包含一个延迟脚本。 async并不保证先后顺序执行
- 注意:现实中,延迟脚本并不一定按照顺序加载,也不一定会在
- 异步加载:模块化手段,
require() / $.getScript()不同的脚本文件,通过 Ajax 加载后,以eval或者<script>标签的形式执行。 - 异步调用:
$(document).ready()事件实现异步调起程序。 - 同步加载:
<script>标签中的代码或者加载的代码。
ES6 Basic Features
- UTF-16 Support
normalize()for more appropriate string comparisons
- Regular Expression
uflag: unicode matchyflag: match at the position specified by itslastIndex
- String.prototype
includes(),startsWith(),endsWith()repeat()
- Object.prototype
Object.is()
- Block Bindings
- use
let
- use
- Constants Declarations
- use
constStatic restrictions prevent use before assignment.
- use
- Destructing Assignment
- Object / Array / Mixed-Types
var [a, ,b] = [2, 3, 4] // a === 2, b === 4var obj = {job: 'a', good: 'b', ok: ['d'], basic: {haha: 'e'}};var {job, good, ok{la}, basic{haha}} = obj;var fn = ({name: x}) => console.log(x)fn({'name': 4}) // 4console.log(job, good, ok, basic); // a b d e
- Numbers
- Octal and Binary Literals
Number.parseInt() / Number.parseFloat()isFinite()andisNaN()
- Math Methods
注释
推荐文件注释
/*!* jQuery JavaScript Library v2.1.4* http://jquery.com/** Includes Sizzle.js* http://sizzlejs.com/** Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors* Released under the MIT license* http://jquery.org/license** Date: 2015-04-28T16:01Z*/
推荐方法注释写法
/*** 发送旺旺/邮件的方法** Doc: http://api.fed.taobao.net/mc** Example:* message(1, '马云', 'Hello')* .then(function(result) {* // 成功逻辑处理* })* .catch(function(err) {* // 错误逻辑处理* });** @param {Number} type 0-旺旺 1-邮件* @param {String} name 旺旺昵称* @param {String} content 消息内容* @return {Promise}* @api public*/function sendMsg(type, name, content) {}
优雅的写法
function hashIt(data) {let hash = 0;const length = data.length;for (let i = 0; i < length; i++) {const char = data.charCodeAt(i);hash = ((hash << 5) - hash) + char;// Convert to 32-bit integerhash &= hash;}}
高级主题
- The JavaScript Runtime
- Stack, Heap, Queue, EventLoop, Worker Thread.
- Garbage Collection
- Reference-counting garbage collection
- IE6,7 Circular Reference Memory Leak
- Mark-and-sweep algorithm
