Literacy and Unicode encoding of JavaScript
in Note with 0 comment
Literacy and Unicode encoding of JavaScript
in Note with 0 comment

Literal

A literal is a constant defined by a grammatical expression or by a lexical expression composed of certain words

A literal is a constant whose value is fixed and cannot be changed during the running of a program script

Array literal

Array literals are a list containing zero or more expressions enclosed in square bracket pairs ([]), where each expression represents an element of the array, and array literals are also array objects.

 var coffees = ["French Roast", "Colombian", "Kona"]; var a=[3]; console.log(a.length); //  one console.log(a[0]); //  three

Extra Comma

 var fish = ["Lion", "Angel"];

In this array, there are two elements that have been assigned values, and an empty element (fish [0] is "Lion", fish [1] is undefined, and fish [2] is "Angel"; note: at this time, the length attribute fish. length of the array is 3)

If you add a comma at the end of the element list, it will be ignored.

 var myList = [ , 'home', 'school'];

The length of the array is 4, and the elements myList [0] and myList [2] are missing

 var myList = ['home', 'school', ];

The length of the array is 4, and the elements myList [1] and myList [3] are missing

Boolean literals

Boolean types have two literals: true and false

integer

Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8)

 0, 117 and -345 (decimal, base 10) 015, 0001 and -077 (octal, base 8)  0x1123, 0x00111 and -0xF1A7 (hexadecimal, "hex" or base 16)

Floating point digital face value

grammar

 [(+|-)][digits][.digits][(E|e)[(+|-)]digits]

example

 three point one four -.2345789 // -0.23456789 -3.12e+12  // -3.12*10^12 .1e-23    // 0.1*10-23=10-24=1e-24

Object literal

Object literals are enclosed in curly braces {} A (element) list of zero or more attribute name value pairs for an object in

 var Sales = "Toyota"; function CarTypes(name) { return (name === "Honda") ? name : "Sorry, we don't sell " + name + "." ; } var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales }; console.log(car.myCar);   //  Saturn console.log(car.getCar);  //  Honda console.log(car.special); //  Toyota

Use a number or string literal as the name of an attribute, or embed a literal inside another literal

 var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" }; console.log(car.manyCars.b); //  Jeep console.log(car[7]); //  Mazda

The object attribute name can be any string, including an empty string. If the object attribute name is not a legal javascript identifier, it must use "" Package. If the name of the attribute is illegal, it can only be marked through the class array [] Access and assignment

 var unusualPropertyNames = { "": "An empty string", "!": "Bang!" } console.log(unusualPropertyNames."");   //  SyntaxError: Unexpected string console.log(unusualPropertyNames[""]);  //  An empty string console.log(unusualPropertyNames.!);    //  SyntaxError: Unexpected token ! console.log(unusualPropertyNames["!"]); //  Bang!

be careful

 var foo = {a: "alpha", 2: "two"}; console.log(foo.a);    //  alpha console.log(foo[2]);   //  two //console.log(foo.2);  //  Error: missing ) after argument list //console.log(foo[a]); //  Error: a is not defined console.log(foo["a"]); //  alpha console.log(foo["2"]); //  two

Regular expression literals

 var re = /ab+c/;

string literal

String literals can contain zero or more characters, surrounded by double quotation marks (") or single quotation marks ('). The following examples are string literals

 "foo" 'bar' "1234" "one line \n another line" "John's cat"

You can use all methods of string objects on string literals, such as using properties similar to String.length on string literals

 "John's cat".length

Use special characters

JavaScript special characters

Character Meaning
\0 Null Byte
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\' Apostrophe or single quote
\" Double quote
\ Backslash character
\XXX The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.
\xXX The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.
\uXXXX The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol.
\u{XXXXX} Unicode code point escapes. For example, \u{2F804} is the same as the simple Unicode escapes \uD87E\uDC04.

Escape character

 var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."; console.log(quote); He read "The Cremation of Sam McGee" by R.W. Service.

Assign the file path c: temp to a string, such as

 var home = "c:\\temp";

Use backslash before line break to escape line break

 var str = "this string \ is broken \ across multiple\ lines. " console.log(str);   //  this string is broken across multiplelines.

You can also escape with the newline character at the end of the line and the escaped newline

 var poem =  "Roses are red,\n\ Violets are blue. \n\ I'm schizophrenic,\n\ And so am I."

Unicode encoding

Unicode is a universal character coding standard, which is used for the exchange and display of the world's major written languages. Unicode allows the exchange, processing and display of multilingual text, as well as the use of general technology and mathematical symbols.

Compatible with ASCII and ISO standards

The UTF-8 encoding of Unicode is compatible with ASCII characters and is supported by many programs. The first 128 Unicode characters correspond to ASCII characters one by one and have the same byte value. Unicode characters from U+0020 to U+007E are equivalent to ASCII characters from 0x20 to 0x7E. ASCII supports Latin letters and uses a 7-bit character set, while each character of UTF-8 occupies one to four 8-bit groups (8-bit octets, that is, one byte or eight bits), which can represent millions of characters.

Unicode escape sequence

You can use Unicode escape sequences in string literals, regular expressions, and identifiers. The escape sequence consists of 6 ASCII characters: u and a 4-digit hexadecimal number.

 var x = "\u00A9 Netscape Communications";

The code returns a copyright symbol and the string "Netscape Communications".

The usage of Unicode escape sequence in JavaScript is different from that in Java:

In JavaScript,

In Java,


Reading materials: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide

Review method: practice (write code once, run once, test once), Google where you don't understand, read and take notes

Bottom line principle: prefer rewriting once rather than copying and pasting

This review includes: literal value and Unicode code

Review time: about 2 hours... I don't know why it takes so long···

Responses