let dessert = 'cake', drink = 'tea' let breakfast =kitchen `今天的早餐是${dessert}與${drink}!`; //定義標籤函數 function kitchen(strings,...values){ //Strings:模版字符串裏每一部分的字符;...values:模版字符串裏的值 console.log(strings); //["今天的早餐是","與","!""] console.log(values); //["cake","tea"] }
function kitchen(strings,...values){ let result =''; for(var i=0;i<values.length;i++){ result += strings[i]; //每次循環讓result加上字符 result += values[i]; //每次循環讓result加上模版字符串裏的插值 } result += strings[strings.length-1]; //result加上最後 一部分字符 return result; } console.log(breakfast) //今天的早餐是cake與tea!