Professional JavaScript for Web Developers 第四版學習筆記 CHAPTER 5: BASIC REFERENCE TYPES
目錄
The Date Type 126
Inherited Methods 128
Date-Formatting Methods 129
Date/Time Component Methods 129
The RegExp Type 131
RegExp Instance Properties 134
RegExp Instance Methods 135
RegExp Constructor Properties 137
Pattern Limitations 139
Primitive Wrapper Types 139
The Boolean Type 141
The Number Type 141
The isInteger() Method and Safe Integers 143
The String Type 144
The JavaScript Character 144
The normalize() Method 147
String-Manipulation Methods 148
String Location Methods 150
String Inclusion Methods 151
The trim() Method 151
The repeat() Method 152
The padStart() and padEnd() Methods 152
String Iterators and Destructuring 152
String Case Methods 153
String Pattern-Matching Methods 153
The localeCompare() Method 156
HTML Methods 157
Singleton Built-in Objects 157
The Global Object 157
URI-Encoding Methods 158
The eval() Method 159
Global Object Properties 160
The Window Object 161
The Math Object 161
Math Object Properties 162
The min() and max() Methods 162
Rounding Methods 163
The random() Method 163
Other Methods 164
Summary 165
-------------------------------------------------
RegExp .lastIndex /g /i /m
g—Indicates global mode, meaning the pattern will be applied to all of the string instead of stopping after the first match is found.
i—Indicates case-insensitive mode, meaning the case of the pattern and the string are ignored when determining matches.
m—Indicates multiline mode, meaning the pattern will continue looking for matches after reaching the end of one line of text.
var re = /cat/g;
for (let i = 0; i < 3; i++) {
console.log(re.test("cate12cate213"));
console.log(re.lastIndex);
}
-------------------------------------------------
RegExp Constructor Properties
let text = "this has been a short summer";
let pattern = /(.)hort/g;
if (pattern.test(text)) {
console.log(RegExp.$_); // this has been a short summer
console.log(RegExp["$`"]); // this has been a
console.log(RegExp["$'"]); // summer
console.log(RegExp["$&"]); // short
console.log(RegExp["$+"]); // s
console.log(RegExp["$*"]); // false
}
let text = "this has been a short summer";
let pattern = /(..)or(.)/g;
if (pattern.test(text)) {
console.log(RegExp.$1); // sh
console.log(RegExp.$2); // t
}
All these RegExp constructor properties are not part of any web standard; avoid use in any production application.
-------------------------------------------------
Primitive Wrapper Types
let numberObject = new Number(10);
let numberValue = 10;
console.log(typeof numberObject); // "object"
console.log(typeof numberValue); // "number"
console.log(numberObject instanceof Number); // true
console.log(numberValue instanceof Number); // false
Boolean,Number,String是唯三的Primitive Wrapper Types。上述方法表現的相似。
-------------------------------------------------
slice(), substr(), and substring()
For slice() and substring(), this second argument is the position before which capture is stopped (all characters up to this point are included except the character at that point).For substr(), the second argument is the number of characters to return.
let stringValue = "0123456789";
console.log(stringValue.slice(3)); // "3456789"
console.log(stringValue.substring(3)); // "3456789"
console.log(stringValue.substr(3)); // "3456789"
console.log(stringValue.slice(3, 7)); // "3456"
console.log(stringValue.substring(3,7)); // "3456"
console.log(stringValue.substr(3, 7)); // "3456789"
There are different behaviors for these methods when an argument is a negative number. For the slice() method, a negative argument is treated as the length of the string plus the negative argument.
let stringValue = "0123456789";
console.log(stringValue.slice(-3)); // "789"
console.log(stringValue.substring(-3)); // "0123456789"
console.log(stringValue.substr(-3)); // "789"
console.log(stringValue.slice(3, -4)); // "345"
console.log(stringValue.substring(3, -4)); // "012"
console.log(stringValue.substr(3, -4)); // "" (empty string)
-------------------------------------------------
indexOf() or lastIndexOf()
Each method accepts an optional second argument that indicates the position to start searching from within the string.…Using this second argument allows you to locate all instances of a substring by looping callings to indexOf() or lastIndexOf()。
let stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
let positions = new Array();
let pos = stringValue.indexOf("e");
while(pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
console.log(positions); // "3,24,32,35,52"
-------------------------------------------------
startsWith(), endsWith(), and includes() ECMAScript 6
let message = "foobarbaz";
console.log(message.endsWith("bar")); // false
console.log(message.endsWith("bar", 6)); // true
trim(),trimLeft() and trimRight()同理。
-------------------------------------------------
repeat()
let stringValue = "na ";
console.log(stringValue.repeat(16) + "batman");
// na na na na na na na na na na na na na na na na batman
-------------------------------------------------
split()
let colorText = "red,blue,green,yellow";
let colors1 = colorText.split(","); // ["red", "blue", "green", "yellow"]
let colors2 = colorText.split(",", 2); // ["red", "blue"]
let colors3 = colorText.split(/[^\,]+/); // ["", ",", ",", ",", ""]
-------------------------------------------------
The min() and max() Methods
let max = Math.max(3, 54, 32, 16);
console.log(max); // 54
let min = Math.min(3, 54, 32, 16);
console.log(min); // 3