首页 > 代码库 > JavaScript- The Good Parts CHAPTER 2
JavaScript- The Good Parts CHAPTER 2
I know it well:
I read it in the grammar long ago.
—William Shakespeare, The Tragedy(悲剧;灾难;惨案) of Titus Andronicus
This chapter introduces the grammar of the good parts of JavaScript, presenting a quick overview of how the language is structured. We will represent the grammar with railroad diagrams.
The rules for interpreting these diagrams are simple:
• You start on the left edge and follow the tracks to the right edge.
• As you go, you will encounter literals in ovals, and rules or descriptions in rectangles.
• Any sequence that can be made by following the tracks is legal.
• Any sequence that cannot be made by following the tracks is not legal.
• Railroad diagrams with one bar at each end allow whitespace to be inserted between any pair of tokens. Railroad diagrams with two bars at each end do not.
The grammar of the good parts presented in this chapter is significantly simpler than the grammar of the whole language.
Whitespace
Whitespace can take the form of formatting characters or comments. Whitespace is usually insignificant, but it is occasionally necessary to use whitespace to separate sequences of characters that would otherwise be combined into a single token. For example, in:
var that = this;
the space between var and that cannot be removed, but the other spaces can be removed.
上图如何看:
下面圈红的都是表示可以走的路
也就这几种类型的可以走,其他都是不允许的
JavaScript offers two forms of comments, block comments formed with /* */ and line-ending comments starting with //. Comments should be used liberally to improve the readability of your programs. Take care that the comments always accurately describe the code. Obsolete(废弃的;老式的) comments are worse than no comments.
The /* */ form of block comments came from a language called PL/I. PL/I chose those strange pairs as the symbols for comments because they were unlikely to occur in that language’s programs, except perhaps in string literals. In JavaScript, those pairs can also occur in regular expression literals, so block comments are not safe for commenting out blocks of code. For example:
/* var rm_a = /a*/.match(s);*/
causes a syntax error. So, it is recommended that /* */ comments be avoided and // comments be used instead. In this book, // will be used exclusively.
Names
A name is a letter optionally followed by one or more letters, digits, or underbars. A name cannot be one of these reserved words:
abstractboolean break bytecase catch char class const continuedebugger default delete do doubleelse enum export extendsfalse final finally float for functiongotoif implements import in instanceof int interfacelongnative new nullpackage private protected publicreturnshort static super switch synchronizedthis throw throws transient true try typeofvar volatile voidwhile with
Most of the reserved words in this list are not used in the language. The list does not include some words that should have been reserved but were not, such as undefined,NaN, and Infinity. It is not permitted to name a variable or parameter with a reserved word. Worse, it is not permitted to use a reserved word as the name of an object property in an object literal or following a dot in a refinement.
Names are used for statements, variables, parameters, property names, operators,and labels.
Numbers
JavaScript has a single number type. Internally, it is represented as 64-bit floating point, the same as Java’s double. Unlike most other programming languages, there is no separate integer type, so 1 and 1.0 are the same value. This is a significant convenience because problems of overflow in short integers are completely avoided, and all you need to know about a number is that it is a number. A large class of numeric type errors is avoided.
If a number literal has an exponent part, then the value of the literal is computed by multiplying the part before the e by 10 raised to the power of the part after the e.So 100 and 1e2 are the same number.
Negative numbers can be formed by using the – prefix operator.
The value NaN is a number value that is the result of an operation that cannot produce a normal result. NaN is not equal to any value, including itself. You can detect NaN with the isNaN(number) function.
The value Infinity represents all values greater than 1.79769313486231570e+308.
Numbers have methods (see Chapter 8). JavaScript has a Math object that contains a set of methods that act on numbers. For example, the Math.floor(number) method can be used to convert a number into an integer.
Strings
A string literal can be wrapped in single quotes or double quotes. It can contain zero or more characters. The \ (backslash) is the escape character. JavaScript was built at a time when Unicode was a 16-bit character set, so all characters in JavaScript are 16 bits wide.
JavaScript does not have a character type. To represent a character, make a string with just one character in it.
The escape sequences allow for inserting characters into strings that are not normally permitted, such as backslashes, quotes, and control characters. The \u convention allows for specifying character code points numerically.
"A" === "\u0041"
Strings have a length property. For example, "seven".length is 5.
Strings are immutable. Once it is made, a string can never be changed. But it is easy to make a new string by concatenating other strings together with the + operator.
Two strings containing exactly the same characters in the same order are considered to be the same string. So:
‘c‘ + ‘a‘ + ‘t‘ === ‘cat‘
is true.
Strings have methods (see Chapter 8):
‘cat‘.toUpperCase( ) === ‘CAT‘
Statements
JavaScript- The Good Parts CHAPTER 2