Javascript 使用let hello=`My age is${age}`但是更新age并不是在这个命令中更新age

Javascript 使用let hello=`My age is${age}`但是更新age并不是在这个命令中更新age,javascript,constants,let,Javascript,Constants,Let,我想设置问候语,这样我就不必再完整地输入了 所以我做了: let hello = `My name is ${name} and I am ${age} years old` 因此,要让我打招呼,我只需键入: console.log(hello); 不过,后来我更新了年龄 ++age; ++age; console.log(hello); 但当我这么做的时候,我再次向自己致意 console.log(hello); “hello”中的“age”没有显示为更新 比如说, let age

我想设置问候语,这样我就不必再完整地输入了

所以我做了:

let hello = `My name is ${name} and I am ${age} years old`
因此,要让我打招呼,我只需键入:

console.log(hello);
不过,后来我更新了年龄

++age;
++age;
console.log(hello);
但当我这么做的时候,我再次向自己致意

console.log(hello);
“hello”中的“age”没有显示为更新

比如说,

let age = 20;
let name = "John";
let hello = `My name is ${name} and I am ${age} years old`
console.log(hello);
…结果是:我叫约翰,今年20岁

然后我更新了年龄

++age;
++age;
console.log(hello);
…但结果仍然是:我叫约翰,今年20岁

原来我有

const hello = `My name is ${name} and I am ${age} years old`
但将“const”改为“let”似乎并没有什么不同

我也试过了

age++;
而不是

++age;
看看我是否错误地记住了该命令,但这两个命令似乎都不起作用。

当您运行时

const hello = `My name is ${name} and I am ${age} years old`
name
age
与字符串“合并”,因此它不会作为变量存储在字符串中,而是作为一个完整的字符串存储。所以你必须这样做,如果你要生成很多这样的字符串,你可以输入一个函数:

let age = 20;
let name = "John";
let hello = `My name is ${name} and I am ${age} years old`
console.log(hello);
age++;
hello = `My name is ${name} and I am ${age} years old`
console.log(hello);
再打个招呼

let年龄=20岁;
让name=“John”;
let hello=`我的名字是${name},我是${age}岁`
console.log(hello);
++年龄;
你好=`我的名字是${name},我是${age}岁`

console.log(hello)这样想:

let a = 2;
let b = 3;
let sum = a + b;
console.log(sum); //writes 5 to the console

a++;
console.log(sum); //still writes 5 to the console

一旦您的
hello
作业运行,
hello
具有静态值,例如
我的名字是约翰,我20岁了
。更改
年龄
不会对
您好
产生任何影响,除非您再次执行此任务:

let age=20
让name=“John”
let hello=`我的名字是${name},我是${age}岁`
log(你好)
年龄++

hello=`我的名字是${name},我是${age}岁`//Ahhh所以基本上我的快捷方式是不可能的。。哈哈。我想我可以重新键入更新的hello,而且不会太多。啊,这是一个令人惊讶的解释。我会尝试不同的选择,但知道这些真的很好。顺便说一下,
age++
++age
如果您将其视为一个命令,则它们是相同的。但是作为一个表达式,
age++
返回旧值(然后作为副作用递增),而
++age
首先递增,然后返回新值。检查
console.log(age++)
vs
console.log(++age)