-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo04.js
51 lines (49 loc) · 1.15 KB
/
demo04.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
function test1() {
function foo(a) {
i = 3
console.log(a + i)
}
for (var i = 0; i < 10; i++) {
foo(i * 2)
}
}
// test1()
function test2() {
let a
console.log(a, typeof a)
console.log(b, typeof b)
var b
console.log(isNaN('a'))
console.log(Number.isNaN('a'))
let n1 = {
a: 1,
}
let n2 = {
a: 1,
}
console.log(Object.is(n1, n2))
}
// test2()
function test3() {
// 仿模板字符串处理功能,
// 如 "Title: ${ title }, MainArtist: ${ artist[0] }, Album: ${ album.name }",
// {
// title: '珊湖海',
// artist: ['周杰伦', '梁心颐'],
// album: {
// publishTime: '2006-11-1',
// name: '十一月的萧邦'
// }
// };
function template(templateString, obj) {
return templateString.replace(/\${([^{}]*)}/g, (match, p1) => eval('obj.' + p1.trim()))
}
const str = 'Title: ${ title }, MainArtist: ${ artist[0] }, Album: ${ album.name }, Time: ${ album.publishTime }'
const obj = {
title: '珊湖海',
artist: ['周杰伦', '梁心颐'],
album: { publishTime: '2006-11-1', name: '十一月的萧邦' },
}
console.log(template(str, obj))
}
test3()