Template Strings
function makeGreeting (name, email, id) {
return 'Hello, ' + name + '. We\'ve emailed you at ' + email '. Your user id is ' + id + '.'
}
- 可以改用如下的Template String就可以不用寫 '+',而且(\')也可以拿掉escape character直接寫('),但括起字串的就不是( ' )而是( ` ),而且變數要用(${})包起來。
function makeGreeting (name, email, id) {
return `Hello, ${name}. We've emailed you at ${email}. Your user id is ${id}.`
}
Default Parameters
- 在function中當某個參數未定義時可能會想給一個預設值,以往會用下列寫法
function debounce (func, wait, immediate) {
if (typeof wait === 'undefined') {
wait = 1000
}
...
}
- 但用Default Parameters的話可以更簡潔
function debounce (func, wait = 1000, immediate) {
...
}
Concise Objects
function getUser (username) {
const email = getEmail(username)
return {
username: username,
email: email
}
}
- 上面那種名稱相同的情況可以用concise object簡化
function getUser (username) {
const email = getEmail(username)
return {
username,
email
}
}
沒有留言:
張貼留言