JavaScript extract Reading Notes (1, 2)

Source: Internet
Author: User
Tags control characters

Chapter 2 Essence
Some of the features of JavaScript bring much more trouble than they do. Some of these features may cause portability problems due to imperfect specifications. Some features may generate codes that are hard to understand and modify; some features make my code style too complex and error-prone; others are design errors. Sometimes Language designers make mistakes.
Most programming languages have excellent components and chicken ribs. I found that I could become a better programmer if I only use the essential part instead of the chicken ribs. After all, how can we build good things with bad parts?
It is almost impossible for the Standards Committee to remove the defective part of a language, because doing so will damage all bad programs that depend on those chicken ribs. In addition to accumulating more features on a large number of existing defects, they are usually powerless. In addition, new and old features do not always coexist harmoniously, which may lead to more chicken ribs.
However, you have the right to define your own subset. You can write better programs based on the essence. The proportion of chicken ribs in JavaScript exceeds expectation. In a very short time, it never evolved into a global adoption. It has never been tried and polished in the lab. When it is still very rough, it is directly integrated into the Navigator 2 browser of the net scene. As Java applets fails, JavaScript becomes the default web page language ". As a programming language, the popularity of JavaScript is almost completely unaffected by its quality.
Fortunately, JavaScript has some excellent parts. The most essential part of JavaScript is deeply hidden, so that the mainstream view of JavaScript over the years is that JavaScript is an ugly and useless toy. The purpose of this book is to reveal the essence of JavaScript and let everyone know that it is an outstanding dynamic programming language.
Perhaps the biggest benefit of learning the essence is that you don't have to consider the chicken ribs. It is very difficult to forget a bad model. This is a very painful job, and most of us are reluctant to face it. Sometimes, a subset of languages is made to help students learn better. But here, I have developed a JavaScript subset to better serve the primary professional.
1.1 why JavaScript?
JavaScript is an important language because it is a web browser language. Its combination with browsers makes it one of the world's most popular programming languages. It is also one of the world's most despised programming languages. The browser's API and Document Object Model (DOM) are rather bad, leading to unfair JavaScript criticism.
JavaScript is the most despised language, because it is not the so-called mainstream language. If you are good at programming in some mainstream languages, but in an environment that only supports JavaScript, it is quite annoying to be forced to use JavaScript. Most people think it is unnecessary to learn JavaScript first, but they are surprised to find that JavaScript is very different from the mainstream languages they prefer to use, and these differences are critical.
The amazing thing about JavaScript is that you can use it to do your work without much understanding of the language or even programming. It is a language with strong expressiveness. When you know what to do, it can even do better. Programming is very difficult. You should never start your work when you know nothing about it.
1.2 analyze JavaScript
JavaScript is built on some very good ideas and a few very bad ideas.
Good ideas include functions, weak types, dynamic objects, and an expressive literal representation. Bad ideas include programming models based on global variables.
JavaScript Functions are top-level objects (mainly) based on lexical scoping. JavaScript is the first mainstream lambda language. In fact, JavaScript has more in common with Lisp and Scheme than Java. It is a Lisp with a C coat. This makes JavaScript a very powerful language.
Nowadays, many programming languages require strong types. The principle is that a strong type allows the compiler to detect errors during compilation. The earlier we can detect and fix errors, the less we will pay. JavaScript is a weak language, so the JavaScript compiler cannot detect type errors. On the other hand, the weak type is actually free. We don't need to create complex times, I never need to force type conversion, and we don't have to cope with the type system to get the desired behavior.
JavaScript has a very powerful representation of object literal. By listing the components of objects, they can be simply created. This representation produces the popular data exchange format-JSON.
Prototype inheritance is a controversial feature in JavaScript. JavaScript has a class-free object system in which objects directly inherit attributes from other objects. This is really powerful, but prototype inheritance is a strange concept for programmers who are trained to use classes to create objects. If you try to apply the class-based design pattern directly to JavaScript, you will suffer setbacks. However, if you learn the prototype nature of using JavaScript, your efforts will be rewarded.
JavaScript has been criticized for its choice of key ideas. Although in most cases, these options are appropriate. However, there is a bad choice: JavaScript depends on global variables for connection. All top-level variables of all compilation units are combined into a public namespace called a global object. This is a bad thing, because global variables are the devil and they are fundamental in JavaScript.
In a few cases, we cannot ignore the chicken ribs. In addition, there are some inevitable dregs. When these components are involved, we will point them out. If you want to learn about the chicken ribs and how to use them poorly, see any other JavaScript books.
JavaScript is a language with many differences. It contains many errors and sharp edges, so you may wonder: "Why do I need JavaScript ?" There are two answers. First, you have no choice. Web has become an important application development platform, and JavaScript is the only language that can be recognized by all browsers. Unfortunately, Java fails in the browser environment. The vigorous development of JavaScript just shows that JavaScript is indeed extraordinary.
The other answer is that JavaScript is excellent despite its flaws. It is lightweight and expressive. And once you have mastered it, you will find functional programming very interesting.
But to better use this language, you must know its limitations. I will reveal them relentlessly. Don't be discouraged. The essence of this language is enough to make up for its shortcomings.
1.3 a simple test site
If you have a Web browser and any text editor, you have everything you need to run JavaScript programs. First, create an HTML file named program.html:
Copy codeThe Code is as follows:
<Html>
<Body>
<Pre>
<Script src = "program. js"> </script>
</Pre>
</Body>
</Html>

Next, create a script file in the same folder and name it program. js:
Document. writeln ('hello, world! ');
Next, use your browser to open your HTML file and view the result. This book always uses a method to define a new method. The following is its definition:
Copy codeThe Code is as follows:
Function. prototype. method = function (name, func ){
This. prototype [name] = func;
Return this;
}

I will explain it in chapter 4th.
Chapter 4 syntax
This chapter introduces the syntax of the essential part of JavaScript and briefly introduces its language structure.
2.1 Blank
Blank spaces may be formatted characters or comments. White spaces are usually meaningless, but occasionally need to be used to separate character sequences, otherwise they will be merged into a single symbol. For example, for the following code:
Var that = this;
Spaces between var and that cannot be removed, but other spaces can be removed.
JavaScript provides two types of Annotations: block comments enclosed by/**/and line comments starting. Annotations should be fully used to improve program readability. Note that annotations must accurately describe the code. Comments that are useless are worse than those without comments.
In the form of block comments enclosed by/**/, it comes from a name called PL/I (silent speech: Short for Programming Language One. The "I" is actually the "one" of the Roman numerals. It is a third-generation advanced programming language invented by IBM in 1850s. In JavaScript, */may appear in the regular expression literal, so block comments are insecure for the commented block. For example:
/*
Var rm_a =/a */. match (s );
*/
A syntax error occurs. Therefore, we recommend that you do not use/**/annotation instead of // annotation.
2.2 identifier
An identifier starts with a letter and can be selectively followed by one or more letters, numbers, or underscores. The following reserved words cannot be used for identifiers:
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
Most reserved words in this list are not used in this language. This list does not contain words that should be reserved but not reserved, such as undefined, NaN, and Infinity. JavaScript does not allow the use of reserved words to name variables or parameters. What's worse, JavaScript cannot use reserved words as the property name of an object in the literal volume of an object, or after the dot of an attribute access expression.
Identifiers are used for statements, variables, parameters, attribute names, operators, and tags.
2.3 digits
JavaScript has only one numeric type. It is represented as a 64-bit floating point number internally, the same as Java's double. In JavaScript, the values 1 and 1.0 are the same.
If a numeric literal has an exponential part, the value of this literal is calculated by multiplying the part before e by the power of the part after e 10. So 100 and 1e2 are the same numbers.
Negative numbers can be composed by the prefix operator.
The value NaN is a numerical value, indicating an operation result that cannot produce normal results. NaN is not equal to any value, including itself. You can use the isNaN (number) function to detect NaN.
The Infinity value indicates all values greater than 1.79769313486231570e + 308.
Digital ownership method (see chapter 1 ). JavaScript has an object Math, which contains a set of methods that act on numbers. For example, you can use the Math. floor (number) method to convert a number into an integer.
2.4 string
The string literal can be enclosed in single quotes or double quotation marks. It may contain 0 or multiple characters. \ Is an escape character. When JavaScript is created, Unicode is a 16-bit character set, so all characters in JavaScript are 16-bit.
JavaScript has no character type. To represent a single character, you only need to create a string containing only one character.
Escape characters can be inserted into strings that are normally not allowed, such as backslash, quotation marks, and control characters. The \ u Convention allows you to specify the digit number.
"A" = "\ u0041"
The string has an ength attribute. For example, "seven". length is 5.
The string is unchangeable. Once a string is created, it cannot be changed. However, it is easy to connect other strings through the + operator to obtain a new string. Two strings that contain identical characters and have the same character sequence are considered to be the same strings. Therefore:
'C' + 'A' + 'T' = 'cat'
Is true.
The string has some methods (see Chapter 8th ).
2.5 statement
A compilation unit contains a set of executable statements. In a web browser, each <script> TAG provides a compilation unit that is compiled and executed immediately. Because the linker is missing, JavaScript will throw them together into a public global namespace. Appendix A has

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.