Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ecmascript 6 让变量和块作用域_Ecmascript 6_Let - Fatal编程技术网

Ecmascript 6 让变量和块作用域

Ecmascript 6 让变量和块作用域,ecmascript-6,let,Ecmascript 6,Let,为什么第一个控制台日志打印出“James”,而它应该打印出“Ken”?让'student'变量作为'if语句'的作用域,并将其值保留为“Ken”吗?另外,当我重新定义同一个变量名“student”时,是否应该出现错误 (function (){ let student = {name: 'James'}; function createStudent(name){ if(true){ let student = {name: name};

为什么第一个控制台日志打印出“James”,而它应该打印出“Ken”?让'student'变量作为'if语句'的作用域,并将其值保留为“Ken”吗?另外,当我重新定义同一个变量名“student”时,是否应该出现错误

(function (){
    let student = {name: 'James'};
    function createStudent(name){
        if(true){
         let student = {name: name};
        }
        return student;
    }
    console.log(createStudent('Ken'));
    console.log(student);
})();

let
是块作用域,因此此行代码:

let student = {name: name};
范围仅限于
if
语句中的括号。那么,你以后做什么

return student;
if
块之外和定义另一个
student
变量的地方之外,该变量不再在范围内,因此范围内唯一的
student
变量是
James
变量

以下是一个注释版本:

(function (){
    let student = {name: 'James'};
    function createStudent(name){
        if(true){
         // define new student variable that is only in scope
         // within this block
         let student = {name: name};
        }
        // here, the student variable on the previous line is no longer
        // in scope so referencing it here sees the first declaration
        return student;
    }
    console.log(createStudent('Ken'));
    console.log(student);
})();
让'student'变量作为'if语句'的作用域,并将其值保留为“Ken”吗

它的块范围是
if
语句。但是,当您执行
返回时,它位于该块之外,因此无法再访问
Ken
学生变量,因此范围内唯一的变量是
James
变量

另外,当我重新定义同一个变量名“student”时,是否应该出现错误


定义已在更高范围内定义的变量不是错误。相反,新声明会隐藏或隐藏该范围内的其他声明,临时覆盖该范围内的声明。

处于块范围,因此此行代码:

let student = {name: name};
范围仅限于
if
语句中的括号。那么,你以后做什么

return student;
if
块之外和定义另一个
student
变量的地方之外,该变量不再在范围内,因此范围内唯一的
student
变量是
James
变量

以下是一个注释版本:

(function (){
    let student = {name: 'James'};
    function createStudent(name){
        if(true){
         // define new student variable that is only in scope
         // within this block
         let student = {name: name};
        }
        // here, the student variable on the previous line is no longer
        // in scope so referencing it here sees the first declaration
        return student;
    }
    console.log(createStudent('Ken'));
    console.log(student);
})();
让'student'变量作为'if语句'的作用域,并将其值保留为“Ken”吗

它的块范围是
if
语句。但是,当您执行
返回时,它位于该块之外,因此无法再访问
Ken
学生变量,因此范围内唯一的变量是
James
变量

另外,当我重新定义同一个变量名“student”时,是否应该出现错误


定义已在更高范围内定义的变量不是错误。相反,新声明会隐藏或隐藏该范围内的其他声明,临时覆盖该范围内的声明。

var
不同的是
let
const
在块语句内创建局部范围。您没有收到错误,因为新变量 阴影
范围之外的一个。

var
不同的是
let
const
在块语句内部创建局部范围。您没有收到错误,因为新变量 阴影
作用域之外的变量。

感谢您解释在if块之外返回变量将如何引用外部作用域的变量赋值。关于重新定义名为'student'的同一变量时为什么没有错误,您的回答是:“定义已在更高范围内定义的变量不是错误。”我在这里看到一个错误:
let a='bar';函数foo(){let a='baz';console.log(a);}console.log(a)没关系-出于某种原因,我在chrome控制台中键入时看到一个错误。但我继续进行了repl.it,效果很好。是因为let变量的作用域不同吗?再次感谢你的帮助!感谢您解释在if块之外返回变量将如何引用外部作用域的变量赋值。关于重新定义名为'student'的同一变量时为什么没有错误,您的回答是:“定义已在更高范围内定义的变量不是错误。”我在这里看到一个错误:
let a='bar';函数foo(){let a='baz';console.log(a);}console.log(a)没关系-出于某种原因,我在chrome控制台中键入时看到一个错误。但我继续进行了repl.it,效果很好。是因为let变量的作用域不同吗?再次感谢你的帮助!要记录'Ken',请在if{}条件内使用return语句。要记录'Ken',请在if{}条件内使用return语句。