"JavaScript Enlightenment" - Reading Notes

By Douglas Crockford
Translator: Dead Moon (Kaidi Zhu)

name

  • [Not recommended] Names starting or ending with an underscore_: usually to represent private properties or global private variables
  • [Not recommended] The dollar sign $: is usually added to variables by some code generators, escapers, and macro processors to ensure that the generated variable names will not conflict with manually written code.
  • [Recommended] All constructor names start with a capital letter, and any other names start with a lowercase letter

value

  • IEEE 754 standard, 64-bit floating point type. 1 sign bit (0 is positive, 1 is negative), 11 exponent bits, 52 effective bits (the default starts with 1, and the actual 53 bits are valid)
  • The idea of ​​floating point numbers: split a number into two parts for storage
    • The first part is the effective digits significantand (or also called coefficient coefficient, fraction fraction, mantissa mantissa)
    • The second part is the exponent, indicating where the decimal point should be inserted in the effective digits.

Boolean type

  • In addition to true and false, there are also truthy and falsy
    • phantom false falsy: false, null, undefined, "" (empty string), 0, NaN
    • Magical Truthy: Everything else

non-class instance object

  • A constructor is a function that returns an object. Its parameters and variables become private properties of the returned object, with no public properties. Some functions inside the constructor become methods that return objects. Private properties are enclosed within closures, while public methods are packaged into the returned frozen object

tail call

  • Tail call: The return value of a function is the direct return value of another function.
  • Tail call optimization cannot be done in a try block. catch will regenerate a scope, the activation object cannot be reduced
  • A new function object is created inside the function, and this new function uses free variables (closures), so it cannot be reduced

purity

  • In functional programming, the return result of a function is only determined by the input. i.e. the same input returns the same result

  • Advantages of pure functions:

    • Better modular design: high cohesion, low coupling
    • more easily tested
    • More powerful composability
    • thread-safe and efficient
  • practice:

    • Drop all assignment operators, as well as var and let statements, leaving only const statements
    • Operators and methods that can modify the contents of objects are discarded
    • Ditch getters and setters
    • Discarding the regular expression exec function will modify the lastIndex attribute
    • for while and do discard, use tail recursion
    • Deprecated Date constructor, Math.random

event-based programming

  • A typical characteristic of sequential programming languages ​​is that they block input and output
  • concurrency
    • Isomorphic concurrency: handle multiple similar operations at the same time, such as arrays provide a pure function that can operate on all elements
    • Heterogeneous concurrency: Support the collaboration of logic items with different responsibilities, so that they can work together as a team
  • An event function is a function that returns immediately, possibly before the work it requires is complete. its result is usually
  • JS is single threaded. Most of the state is not on the stack, but in the closure of the function
  • Event loop mechanism

date

JSON

  • The origin of the name of JSON (JavaScript Object Notation).
  • If the object contains a toJSON method, then toJSON's own method will be executed. Generally speaking JSON.stringify will call this method
  • Optional parameter replacer for JSON.stringify:
    • If a function, it takes a key and a value, and returns the final value for the key
    • If it is an array of strings, only the properties in the array will be transformed (filter)

test

  • We can only prove that the program is flawed, but not that it is flawless
  • Bug is synonymous with chaos. Eliminating clutter increases productivity more than testing
  • Usually develop the habit of deleting more codes. When estimating the development cycle, be sure to allow time for removing redundant code and eliminating problematic dependencies
  • False negatives can always be caught and fixed quickly, while false positives can hide for a long time
  • As purity decreases, bugs increase, and unit tests do not test code purity

optimization

  • We should only optimize where there are significant results
  • The purpose of optimization is to save time, we must optimize our optimization
  • A very common practice is to compare two features of a language by writing them separately into a loop and then measuring the loop time. There is a problem with this way:
    • Test feature performance, or the degree of engine optimization
    • Are the results portable?
    • Can other engines have the same result
    • Will the result change in the future?
  • In the programming world: think before you act, review the past and learn the new
  • There are almost no performance problems in the current code, and optimizing code at non-performance bottlenecks is a waste of life
  • Low performance culprits:
    • cannot be parallelized
    • violation of the rules of the round
    • low cohesion
    • high coupling
    • wrong algorithm
    • cache thrashing
    • code bloat
    • third party code
    • Network resources are loaded sequentially, not pipelined
    • Extremely inefficient DOM API

translate

  • A transpilation is a special kind of compilation that transforms one programming language into another.
  • Advantages of transpilers (like babel)
    • experiment
    • specific
    • Compatible with old versions
    • fashionable
    • precedence
    • safe (remove unsafe features)
    • Performance (supports ASM, WASM, etc.)
  • Never use transpilers in production
  • The characteristics that the next language should meet:
    • Better support for Unicode
    • Use UTF-32 as the internal representation of characters
    • Direct support for binary large objects (Blobs)
    • Better support for event-based programming
    • Better support for secure networks
    • Better support for process management, including startup, communication, and destruction
    • Support for parallel processing of pure functions

Compiler principle

  • Word segmentation (dividing various tokens)
  • Parse (convert token to AST)
  • code generation
  • Runtime: the software used to execute the program
    • If a source language differs semantically from its target language, some special runtime support is required

Guess you like

Origin blog.csdn.net/sinat_36521655/article/details/126653478