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:

abstract
boolean break byte
case catch char class const continue
debugger default delete do double
else enum export extends
false final finally float for function
goto
if implements import in instanceof int interface
long
native new null
package private protected public
return
short static super switch synchronized
this throw throws transient true try typeof
var volatile void
while 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

时间: 2024-10-02 13:33:28

JavaScript- The Good Parts CHAPTER 2的相关文章

JavaScript: The Evil Parts - 1

最近在看JavaScript框架设计,在讲解类型判定的时候提到了一些“匪夷所思的情况”,不过没有明说都是什么时候会出现这些情况.自己玩儿了一下,写写随笔吧.不过可能除了我找到的,还有会其他时候会出现这些诡异的现象2333 问题:在JavaScript中,什么时候会出现 a !== a a == b && b != a a == !a a == b && a == c && b != c a != b && a != c &&

JavaScript- The Good Parts Chapter 4

Why, every fault’s condemn’d ere it be done:Mine were the very cipher of a function. . .—William Shakespeare, Measure for Measure The best thing about JavaScript is its implementation of functions. It got almost everything right. But, as you should e

JavaScript: The Good Parts

Chapter 1 Good Parts: JavaScript is an important language because it is the language of the web browser. The very good ideas include functions, loose typing, dynamic objects, and an expressive object literal notation. The bad ideas include a programm

JavaScript- The Good Parts Chapter 6

Thee(你) I’ll chase(追逐:追捕) hence(因此:今后), thou(你:尔,汝) wolf in sheep’s array.—William Shakespeare, The First Part of Henry the SixthAn array is a linear(线的,线型的:直线的,线状的:长度的) allocation(分配,配置:安置) of memory in which elements are accessed by integers that a

JavaScript- The Good Parts Chapter 3 Objects

Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple types of JavaScript are numbers, strings, booleans (true and false), null,and undefined. All other values are objects. Numbers, strings, and booleans are 

JavaScript- The Good Parts Chapter 5 Inheritance

Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but confusion. . .—William Shakespeare, The Tragedy of King Richard the Second Inheritance is an important topic in most programming languages. In the cl

读 《JavaScript: The Good Parts》 有感

提炼出一门语言或技术的 Good Parts, 使用该子集去构造健壮稳固的应用. 我们总是倾向于去学习和使用所有的语言特性,好像凡是新的,凡是提供了的, 就有必要去使用: 这本书告诉我们, 要有选择性地学习和使用. 不是所有的语言特性都需要学习和使用. 学习和使用那些设计不良的特性,不仅耗费大量时间和精力,而且有损项目的可维护性,得不偿失:反之,学习那些优良的部分,不仅可以节省时间,腾出更多时间和精力去做更重要的事情,而且有助于将事情做好,提高收益/学习比. 富有技巧性的HACKER可以欣赏其精

《JavaScript高级程序设计》Chapter 15 canvas + Chapter 16 HTML5

Chapter 15 Canvas Chapter 16 HTML5 Chapter 15 Canvas <canvas>元素:设定区域.JS动态在这个区域中绘制图形. 苹果公司引导的.由几组API构成. 2D上下文普及了.WebGL(3D上下文)还未足够普及. 基本用法 首先:width.height属性确定绘图区域大小.后备信息放在开始和结束标签之间. getContext():DOM获得这个canvas元素对象.再在这个对象上调用getContext()获取上下文,传入参数表示获取的是2

《JavaScript高级程序设计》Chapter 14 表单脚本

之前就提到了,JS创建之初是为了减少服务器的压力,而当时这个压力主要体现在表单的验证上.与此同时,JS还为WEB表单增加了一些行为. 表单基础知识 go 文本框脚本 go 选择框脚本 go 表单序列化 go 富文本编辑 go JS表单基础知识 JS中获取表单元素(<form>)的方法: 通过id(所有元素通用)getElementById(""); 通过document.form:document.forms[0] 或者 document.forms["formN