Fed*_*eSc 3 javascript template-literals
I'm trying to re-use a template literal in javascript.
This isn't working:
let int;
let temp= `Number is: ${int}`;
console.log(temp)
// Number is: undefined
int = 1;
console.log(temp)
// Number is: undefined
int = 2;
console.log(temp)
// Number is: undefined
Run Code Online (Sandbox Code Playgroud)
I thought that changing int variable will dynamicaly change in the template. but i learned otherwise :D
Is it even possible and if so what is the right way of doing it?
Thanks.
Simply make it to be returned by a function:
let int;
let temp= () => `Number is: ${int}`;
console.log(temp())
// Number is: undefined
int = 1;
console.log(temp())
// Number is: 1
int = 2;
console.log(temp())
// Number is: 2Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |