JavaScript 语言核心

“所以在设计产品和编程语言时,我们希望直接使用核心的精华部分,是因为这些精华创造了大部分价值”

学习参考资料:

基本结构

基础概念

  • 严格上来说:脚本中应该启动 "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
    • u flag: unicode match
    • y flag: match at the position specified by its lastIndex
  • String.prototype
    • includes(), startsWith(), endsWith()
    • repeat()
  • Object.prototype
    • Object.is()
  • Block Bindings
    • use let
  • Constants Declarations
    • use const Static restrictions prevent use before assignment.
  • Destructing Assignment
    • Object / Array / Mixed-Types
  1. var [a, ,b] = [2, 3, 4] // a === 2, b === 4
  2. var obj = {job: 'a', good: 'b', ok: ['d'], basic: {haha: 'e'}};
  3. var {job, good, ok{la}, basic{haha}} = obj;
  4. var fn = ({name: x}) => console.log(x)
  5. fn({'name': 4}) // 4
  6. console.log(job, good, ok, basic); // a b d e
  • Numbers
    • Octal and Binary Literals
    • Number.parseInt() / Number.parseFloat()
    • isFinite() and isNaN()
  • Math Methods

注释


推荐文件注释

  1. /*!
  2. * jQuery JavaScript Library v2.1.4
  3. * http://jquery.com/
  4. *
  5. * Includes Sizzle.js
  6. * http://sizzlejs.com/
  7. *
  8. * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors
  9. * Released under the MIT license
  10. * http://jquery.org/license
  11. *
  12. * Date: 2015-04-28T16:01Z
  13. */

推荐方法注释写法

  1. /**
  2. * 发送旺旺/邮件的方法
  3. *
  4. * Doc: http://api.fed.taobao.net/mc
  5. *
  6. * Example:
  7. * message(1, '马云', 'Hello')
  8. * .then(function(result) {
  9. * // 成功逻辑处理
  10. * })
  11. * .catch(function(err) {
  12. * // 错误逻辑处理
  13. * });
  14. *
  15. * @param {Number} type 0-旺旺 1-邮件
  16. * @param {String} name 旺旺昵称
  17. * @param {String} content 消息内容
  18. * @return {Promise}
  19. * @api public
  20. */
  21. function sendMsg(type, name, content) {
  22. }

优雅的写法

  1. function hashIt(data) {
  2. let hash = 0;
  3. const length = data.length;
  4. for (let i = 0; i < length; i++) {
  5. const char = data.charCodeAt(i);
  6. hash = ((hash << 5) - hash) + char;
  7. // Convert to 32-bit integer
  8. hash &= hash;
  9. }
  10. }

高级主题