let 定义的变量,在块级作用域内,只能定义一次,否者会提示XX变量 has already been declared 已经被定义了(注意点:let定义的变量,可以修改所定义的值)
const定义的变量,具有块级作用域,在块级作用域内只能定义一次,否者提示已被定义了,值得注意的是const定义的变量是只读的,不可修改的,否者会提示 XXX is read-only
<template>
<div></div>
</template>
<script>
export default {
data() {
return {};
},
created() {
this.init();
},
methods: {
init() {
{
var b = 10;
let c = 20;
const d = 30;
console.log("内b", b, "内c", c,"内d",d);//内b 10 内c 20 内d 30
{
console.log("第二层内c",c); // 第二层内c 20
}
}
console.log("外b", b,);//外b 10
console.log("外c", c); // 报错 c is not defined found in
console.log("外d", d);// 报错 d is not defined found in
},
},
};
</script>