javascript - Template literals (Template strings) didn't give the exact result in arrow function
问题描述
I am following a medium article about javascript es 6 template literals. But it didn't give me the result, but I type the exact code that showed in the article. I think maybe something silly that I had mistaken, but didn't able to find out.
const myFunctn=(name,age)=>{
return 'Hi ${name} , you are ${age} years old';
}
console.log(myFunctn('Said',22));
ok i think the output should have
Hi Said, you are 22 years old
But my output in the chrome browser console is below
Hi ${name} , you are ${age} years old
I had checked that ECMAScript is enabled in my browser, so that is not the issue. Maybe I had make some stupid mistakes.
解决方案
您需要反引号作为模板文字的分隔符。
const myFunctn = (name, age) => {
return `Hi ${name} , you are ${age} years old`;
}
console.log(myFunctn('Said', 22));
推荐阅读
- azure-devops - 无需代码签出即可创建 Azure Devops 管道
- qt - 如何在qt qml中更改地图上代表组件的颜色
- node.js - 如何使 MongoDB 更新执行得更快?
- reactjs - 在本机反应中动态呈现问题
- java - 可变 Spring Bean 中的线程安全
- python - 从 Visual Studio Code 中的覆盖范围调用的 Python 单元测试中的触发断点
- reactjs - 获取 TypeError:无法获取
- reactjs - 为什么 React 组件渲染只完成一次,而 reduce-action 会多次更改状态?
- python - Python中的返回问题
- c# - C#:如何操作列表中项目的值