An introduction to JavaScript and its main features

Introduction to the

JavaScript is a function-first, lightweight, interpreted or just-in-time compiled programming language

The main function

  1. Embed dynamic text in HTML pages
  2. Responds to browser events
  3. Reading and writing HTML elements
  4. Validate data before it is submitted to the server
  5. Detect browser information of visitors, control cookies, including creation and modification, etc
  6. Server-side programming based on Node.js technology

The birth of JavaScript

JavaScript was originally designed by Brendan Eich of Netscape and originally named its scripting language LiveScript, but Netscape changed its name to JavaScript after partnering with Sun to capitalize on Java’s popularity

In general, Brendan Eich’s design ideas are mainly as follows:

  • Learn from the basic syntax of C language
  • Borrow data types and memory management from the Java language
  • Use the Scheme language to elevate functions to “First class” status
  • Reference Self language, using prototype – based inheritance mechanism

Ten Design flaws of JavaScript

1. Not suitable for developing large programs

  • JavaScript has no namespace and is difficult to modularize
  • No specification for how to distribute code across multiple files
  • Allows for duplicate definitions of functions with the same name, with the latter overwriting the previous, making modular loading difficult

2. Very small standard libraries

  • The standard library of functions provided by JavaScript is very small and can only do a few basic operations, with many functions lacking

3. The null, and undefined

  • Null is a type of object, which means that the object is empty. Undefined is a data type, which means undefined
typeof null; // object
typeof undefined; // undefined
Copy the code
  • It’s very confusing, but it has very different meanings
var foo;
alert(foo == null); // true
alert(foo == undefined); // true
alert(foo === null); // false
alert(foo === undefined); // true
Copy the code

4. Global variables are difficult to control

  • JavaScript global variables that are visible in all modules
  • Global variables can be generated inside any function, which greatly increases the complexity of the program.
a = 1; (function(){ b=2; alert(a); }) (); // 1 alert(b); / / 2Copy the code

5. Automatically insert semicolons at the end of lines

  • All JavaScript statements must end with a semicolon
  • However, if you forget to add a semicolon, the interpreter automatically adds the semicolon for you instead of reporting an error, sometimes leading to hard-to-find errors

For example, the following function does not achieve the desired result at all. Instead of returning an object, it returns undefined

Function (){return {I =1}; }Copy the code

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

6. The plus operator

  • As an operator, the + sign has two meanings. It can represent the sum of numbers or the concatenation of characters
  • If one operation item is a character and the other is a number, the number is automatically converted to a character
  • Such a design unnecessarily increases the complexity of the operation, and a separate concatenation operator can be set up

7. NaN

  • NaN is a number that means beyond the limits of the interpreter, and it has some very strange features
NaN === NaN; //false NaN ! == NaN; //true alert( 1 + NaN ); // NaNCopy the code

Instead of designing NaN, the interpreter can simply report errors

8. Distinction between arrays and objects

  • Since JavaScript arrays are objects, it can be tricky to tell whether an object is an array or not
If (arr && typeof arr === 'object' && Typeof arr.length === 'number' &&! arr.propertyIsEnumerable('length')){ alert("arr is an array"); }Copy the code

9. = = and = = =

  • == is used to determine whether two values are equal. When two values are of different types, automatic conversion occurs, and the result is very counterintuitive
  • Therefore, it is recommended to use the “===” (exact judgment) comparator at all times
0 == "" // true 0 == "" // true false == "" // false false == "0" // true false == undefined // false False == null // false null == undefined // true "\t\r\n" == 0 // trueCopy the code

10. Wrapper objects of basic types

  • JavaScript has three basic data types: strings, numbers, and Booleans
  • They all have constructors that generate string objects, number objects, and Booleans
new Boolean(false); new Number(1234); new String("Hello World");Copy the code

The object types that correspond to the base data types are of little use but of great confusion

alert( typeof 1234); // number alert( typeof new Number(1234)); // objectCopy the code