-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 函数参数的默认值 | ||
``` | ||
function multiply(a, b) { | ||
a = a || 5; | ||
b = b || 3; | ||
return a * b; | ||
} | ||
``` | ||
ES6可以写成 | ||
``` | ||
function multiply(a=5, b=6) { | ||
return a * b; | ||
} | ||
``` | ||
* 如果只想传入第二个参数,第一个参数应该是 `undefined` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 模板字符串 | ||
|
||
ES6语序我们用反引号 ` ` \` 来定义我们的字符串,可以不需要用 `+` 来拼接字符串。如果字符串里面需要使用到变量的时候可以使用`${variable}`,甚至可以在 `{}` 中使用js表达式。 | ||
|
||
``` | ||
const dp = { | ||
name: 'dp', | ||
todos: [ | ||
{ name: 'go to store', completed: false }, | ||
{ name: 'watch movie', completed: false }, | ||
{ name: 'running', completed: true }, | ||
] | ||
} | ||
const templete = ` | ||
<ul> | ||
${dp.todos.map(todo => `<li>${todo.name} ${todo.completed ? '√' : 'X'}</li>`).join('')} | ||
</ul> | ||
`; | ||
console.log(templete); | ||
``` | ||
|
||
# 标签模板字符串 | ||
|
||
``` | ||
function highlight(strings, ...values) { | ||
const highlighted = values.map(value => `<span class="highlight">${value}</span>`); | ||
return strings.reduce((prev, curr, i) => `${prev}${curr}${highted[i] || ''}`, ''); | ||
} | ||
const user = 'Marry'; | ||
const topic = 'Learn to use markdown'; | ||
const sentence = highlight`Dp ${user} has commented on your topic ${topic}`; | ||
document.body.innerHTML = sentence; | ||
``` |