《Eloquent JavaScript》读后感

Last Updated:

2022-12-23

简介

ECMAScript 历史(es3->es5->es6...)

There have been several versions of JavaScript. ECMAScript version 3 was the widely supported version in the time of JavaScript’s ascent to dominance, roughly between 2000 and 2010

basically no version 4(2008), comes version 5(2009)

es6(ECMAScript version 6 came out in 2015, is a major update)

work on the version 4 was abandoned in 2008, leading to a much less ambitious version 5, which made only some uncontroversial improvements, coming out in 2009. Then in 2015 version 6 came out, a major update that included some of the ideas planned for version 4. Since then we’ve had new, small updates every year.

第三章,函数

递归函数,求结果(感觉就像二叉树遍历)

^33c96f

问题描述:

Consider this puzzle: by starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite set of numbers can be produced. How would you write a function that, given a number, tries to find a sequence of such additions and multiplications that produces that number?

答案:

function findSolution(target) {
  function find(current, history) {
    if (current == target) {
      return history;
    } else if (current > target) {
      return null;
    } else {
      return find(current + 5, `(${history} + 5)`) ||
             find(current * 3, `(${history} * 3)`);
    }
  }
  return find(1, "1");
}

console.log(findSolution(24));
// → (((1 * 3) + 5) * 3)

上述答案,不求最优解,但求有解

第四章,数据结构:对象和数组Data Structures: Objects and Arrays

第五章,函数: Higher-Order Functions

UTF-16, the format used by JavaScript strings

It describes most common characters using a single 16-bit code unit but uses a pair of two such units for others.

UTF-16 is generally considered a bad idea today.

UTF-16, 问题在于,不知道一个characters是使用一个unit,还是2个unit。

譬如,一个string,里头有"absc"等占位一个unit的character,也有emoji等占位2个unit的character。 那么,我们怎么准确知道这串文本(string),导致应该多长("length")

使用 for/of + codePointAt 的组合方式来获取

codePointAtcharCodeAt的区别(他们的入参都是index)

JavaScript’s charCodeAt method gives you a code unit, not a full character code. The codePointAt method, added later, does give a full Unicode character.