Professional JavaScript for Web Developers 第四版學習筆記 CHAPTER 3 LANGUAGE BASICS
目錄
Syntax 25
Case-Sensitivity 26
Identifiers 26
Comments 26
Strict Mode 27
Statements 27
Keywords and Reserved Words 28
Variables 29
The ’var’ Keyword 29
var Declaration Scope 29
var Declaration Hoisting 30
’let’ Declarations 31
Temporal Dead Zone 32
Global Declarations 32
Conditional Declaration 32
let Declaration in for Loops 34
’const’ Declarations 34
Declaration Styles and Best Practices 35
Don’t Use var 36
Prefer const Over let 36
Data Types 36
The typeof Operator 36
The Undefined Type 37
The Null Type 39
The Boolean Type 40
The Number Type 41
Floating-Point Values 41
Range of Values 42
NaN 43
Number Conversions 44
The String Type 46
Character Literals 47
The Nature of Strings 48
Converting to a String 48
Template Literals 49
Interpolation 50
Template Literal Tag Functions 51
Raw Strings 53
The Symbol Type 54
Basic Symbol Use 54
Using the Global Symbol Registry 55
Using Symbols as Properties 56
Well-Known Symbols 57
The Object Type 67
Operators 68
Unary Operators 68
Increment/Decrement 68
Unary Plus and Minus 70
Bitwise Operators 72
Bitwise NOT 73
Bitwise AND 74
Bitwise OR 74
Bitwise XOR 75
Left Shift 76
Signed Right Shift 76
Unsigned Right Shift 76
Boolean Operators 77
Logical NOT 77
Logical AND 78
Logical OR 79
Multiplicative Operators 80
Multiply 80
Divide 81
Modulus 82
Exponentiation Operator 82
Additive Operators 82
Add 83
Subtract 84
Relational Operators 85
Equality Operators 86
Equal and Not Equal 86
Identically Equal and Not Identically Equal 87
Conditional Operator 88
Assignment Operators 88
Comma Operator 89
Statements 90
The if Statement 90
The do-while Statement 90
The while Statement 91
The for Statement 91
The for-in Statement 92
The for-of Statement 93
Labeled Statements 93
The break and continue Statements 94
The with Statement 95
The switch Statement 96
Functions 98
Summary 100
Template Literals
let value = 5;
let exponent = 'second';
// Formerly, interpolation was accomplished as follows:
let interpolatedString = value + ' to the ' + exponent + ' power is ' + (value * value);
// The same thing accomplished with template literals:
let interpolatedTemplateLiteral = `${ value } to the ${ exponent } power is ${ value * value }`;
console.log(interpolatedString); // 5 to the second power is 25
console.log(interpolatedTemplateLiteral); // 5 to the second power is 25
ECMAScript 6定義的新功能。Interpolation,Template Literal Tag Functions,Raw Strings這些都是結合起來使用的。
-------------------------------------------------
The Symbol Type
let s1 = Symbol('foo'), s2 = Symbol('bar'), s3 = Symbol('baz'), s4 = Symbol('qux');
let o = {
[s1]: 'foo val'
};
// Also valid: o[s1] = 'foo val';
console.log(o);
// {Symbol{foo}: foo val}
New in ECMAScript 6 is the Symbol data type. Symbols are primitive values, and symbol instances are unique and immutable. The purpose of a symbol is to be a guaranteed unique identifier for object properties that does not risk property collision.
Well-Known Symbols:Symbol.asyncIterator、Symbol.hasInstance、Symbol.isConcatSpreadable、Symbol.iterator、Symbol.match、Symbol.replace、Symbol.search、Symbol.species、Symbol.split、Symbol.toPrimitive、Symbol.toStringTag、Symbol.unscopables。
-------------------------------------------------
Exponentiation Operator(指數操作符)
console.log(Math.pow(3, 2));
console.log(3 ** 2);//9
let squared = 3;
squared **= 2;
console.log(squared); // 9
ECMAScript 7出現的新東西, Math.pow()的數據操作符號。
-------------------------------------------------
字符串比較結果
let result = "23" < "3"; // true
For strings, each of the first string’s character codes is numerically compared against the character codes in a corresponding location in the second string.
-------------------------------------------------
The do-while Statement
let i = 0;
do {
i += 2;
} while (i < 0);
代碼至少執行一次;它是post-test;while是pretest。
-------------------------------------------------
The for-in Statement
for (const propName in window) {
console.log(propName);
}
It is used to enumerate the non-symbol keyed properties of an object;
-------------------------------------------------
The for-of Statement
for (const el in [2,4,6,8]) {
console.log(el);
}
It is used to loop through elements in an iterable object.