2024-05-21|閱讀時間 ‧ 約 22 分鐘

※ 物件導向程式設計-OPP(四)

    ※ 模板字串(template literal):

    • 使用「``」作為語法,作為字串拼接。
    • 呈現多行內容。
    const multiLineString = `This is a string
    that spans multiple
    lines.`
    console.log(multiLineString)
    /*
    This is a string
    that spans multiple
    lines.
    */
    • 可使用${var}嵌套變數和任意表達式。
    const a = 5
    const b = 10
    const result = `The sum of ${a} and ${b} is ${a + b}.`
    console.log(result) // The sum of 5 and 10 is 15.
    • 調用函數
    function greet(name) {
    return `Hello, ${name}`
    }

    const message = `${greet('Bob')}! How are you?`
    console.log(message) // Hello, Bob! How are you?
    分享至
    成為作者繼續創作的動力吧!
    © 2024 vocus All rights reserved.