-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.js
91 lines (62 loc) · 1.85 KB
/
string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* strings and regular expressions
*
* template literals (模板字面量)
*
* 我们常常在代码里面会有一个操作是 用获取到的数据来拼接 html字符串
*
*
*
*/
/**
* 基础语法:
* 反引号 包裹
*/
let message = `today is 11.11`;
console.log(message);
console.log(typeof message);
//如果字符串内想包含反引号的话,用\转义
message = `today is \` 11.11 \``;
console.log(message);
/**
* 先有问题:
* 创建 多行字符串
*/
//es5时代 用单引号或者多引号 创建的字符串 是单行的
message = 'tody\
is\
11.11\
';
console.log(message);
//es5解决办法 加一个换行的转义字符
message = 'tody \n\
is\n\
11.11';
console.log(message);
/**
* 替换 ${} 包裹 任何合法的js表达式
*
* 列举 表达式 包括哪些 calculations function call
* 常见的哪些是:literals(null true false array object), function defining expressions, template literals, grouping operator, property accessors, new operator, function calls, delete operator,void operator, typeof opetator, + - ! operator, conditional operator, comma operator
* 常见的哪些不是:if 语句,循环语句,switch 语句, return
*/
let date = '11.12';
message = `today is ${date}`;
console.log(message);
//
let count = 10;
let price = 100;
message = `today is ${date}, i payed ¥${(count * price).toFixed(2)}`;
console.log(message);
//条件语句
//message = `today is ${date}, i payed ¥ ${ if (count > 0) count * price }`;
//console.log(message);
count = 0;
message = `today is ${date}, i payed ${ count > 0 ? '¥' + (count * price).toFixed(2) : 'no money' }`;
console.log(message);
//循环语句
//message = `today is ${date}, i payed ${ return '¥1000'; }`;
//console.log(message);
//
//
//tag