4-1 JS overview (birth of JavaScript, standard customization, ten defects)

Posted by Vince on Fri, 04 Mar 2022 11:13:10 +0100

1. How to count your own code lines
Install: yarn global add cloc
(cloc: count lines of code)
Statistics: cloc --vcs=git
(. Indicates the current directory of analysis)
2. Sir Lee invented HTML
Mr Lai invented CSS
Brandon invented JS

1, The birth of JavaScript
1. Brandon was ordered in the face of danger

  • The company requires adding a script function to the browser
  • Scripts must rub against Java traffic
  • Brandon spent ten days designing the first version of JS

2. Naming of JS

  • Mocha Mocha = > livescript = > JavaScript
  • Java is both a programming language and a kind of coffee
  • At first, the browser supported both Java and JavaScript, and then JS won (on the browser)

2, Customization of ECMAScript standard
1. Time

  • In June 1997, the first version of ECMAScrip t was released
  • In December 1999, the third version was released. This version is the most widely used (IE6 support)
  • Fourth edition abortion
  • In December 2009, the fifth edition was released, adding some new functions
  • In June 2015, the sixth edition was released, and all new browsers support this edition

2. Relationship between JS and ECMAScrip t
ECMAScrip is a paper standard, and JS is the implementation of browser
The IQ standard often lags behind the browser. It is implemented first and then written into the standard

3.3 rise of JavaScript
Killer app Gmail

  • In 2004, Gmail online website
  • In 2005, Jesse named the technology used by Google AJAX (play concept, no new things, take a new name)
  • Since then, the front-end technology has officially appeared. Before that, web development was completed by the back-end and designers
  • In 2006, jQuery was released, which is the oldest JS Library (browser compatible)

Ten defects of JavaScript
(reproduced in Ruan Yifeng: Ten defects of JavaScript)

  1. Not suitable for developing large programs
    Javascript has no namespace and is difficult to modularize; There is no specification for how to distribute code in multiple files; Duplicate definitions of functions with the same name are allowed. The later definitions can overwrite the previous definitions, which is not conducive to modular loading.

  2. Very small standard library
    The standard function library provided by Javascript is very small, which can only complete some basic operations, and many functions are not available.

  3. null and undefined
    null is a kind of object, which means that the object is empty; Undefined is a data type, which means undefined.

typeof null; // object
typeof undefined; // undefined

The two are very confusing, but they have completely different meanings.

  var foo;
  alert(foo == null); // true
  alert(foo == undefined); // true
  alert(foo === null); // false
  alert(foo === undefined); // true

In programming practice, null is almost useless and should not be designed at all.

  1. Global variables are difficult to control
    Global variables of Javascript are visible in all modules; Any function can generate global variables, which greatly aggravates the complexity of the program.
  a = 1;
  (function(){
    b=2;
    alert(a);
  })(); // 1
  alert(b); //2
  1. Auto insert line ending semicolon
    All statements in Javascript must end with a semicolon. However, if you forget to add a semicolon, the interpreter does not report an error, but automatically adds a semicolon for you. Sometimes, this can lead to some hard to find errors.
    For example, the following function cannot achieve the expected result at all. The return value is not an object, but undefined.
  function(){
    return
      {
        i=1
      };
  }

The reason is that the interpreter automatically adds a semicolon after the return statement.

  function(){
    return;
      {
        i=1
      };
  }
  1. Plus operator
    +As an operator, sign has two meanings. It can represent the sum of numbers and characters, or the connection between characters.
  alert(1+10); // 11
  alert("1"+"10"); // 110

If one operation item is a character and the other operation item is a number, the number is automatically converted to a character.

  alert(1+"10"); // 110
  alert("10"+1); // 101

This design unnecessarily aggravates the complexity of operation, and a character connection operator can be set separately.

  1. NaN
    NaN is a number indicating that the limit of the interpreter has been exceeded. It has some strange features:
  NaN === NaN; //false
  NaN !== NaN; //true
  alert( 1 + NaN ); // NaN

Instead of designing NaN, it is better for the interpreter to report errors directly, which is conducive to simplifying the program.

  1. Distinction between arrays and objects

Because Javascript arrays also belong to objects, it is quite troublesome to distinguish whether an object is an array or not. Douglas Crockford's code is as follows:

  if ( arr &&
    typeof arr === 'object' &&
    typeof arr.length === 'number' &&
    !arr.propertyIsEnumerable('length')){
    alert("arr is an array");
  }
  1. ==And===
    ==Used to judge whether two values are equal. When the two value types are different, automatic conversion will occur, and the result is very inconsistent with intuition.
  "" == "0" // false
  0 == "" // true
  0 == "0" // true
  false == "false" // false
  false == "0" // true
  false == undefined // false
  false == null // false
  null == undefined // true
  " \t\r\n" == 0 // true

Therefore, it is recommended to use "= =" (accurate judgment) comparator at any time.

  1. Wrapper object of basic type
    Javascript has three basic data types: string, number, and Boolean. They all have corresponding constructor functions, which can generate string objects, numeric objects and Boolean objects.
  new Boolean(false);
  new Number(1234);
  new String("Hello World");

The object type corresponding to the basic data type has little effect, but causes great confusion.

  alert( typeof 1234); // number
  alert( typeof n

Topics: Javascript