JS写for-of/in循环的注意事项

本文通过实际案例,探讨了在ES6中使用for循环时let关键字的重要性。作者发现,省略let会导致循环变量成为全局变量,而在严格模式下会引发错误。正确使用let可以避免变量污染,确保循环结束后变量正常销毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

菜鸟教程 误导了,之前我写for-of/in循环中的迭代变量一直都是不加标识符的
这是菜鸟教程中关于JS的一个教程
也就是直接就for(x in person)
然后我今天在看《深入理解es6》的时候注意到了let
在这里插入图片描述
突然想到一点,之前的for循环却是用了let的。
for(let i = 0; i++; i <=10),
如果写成for(i = 0; i++; i<=10), 其实是创建了一个全局变量 i ,循环结束了,这个 i 也不会被销毁,应该会等于11.(写循环的时候被坑过)。
既然for循环都要加let,无疑for-of或者for-in也应该要加let
为了验证这个猜想,我使用了严格模式,果然是报错了
在这里插入图片描述
然后验证了一下不加let是否声明了全局变量
在这里插入图片描述
num在循环外也没被销毁,而且值是3.
最后测试了一下标准写法
在这里插入图片描述
此处n is not defined证实了猜想,循环结束n销毁了

aurora@aurora:~/rs485_static_ws$ catkin_make \ > -DCMAKE_PREFIX_PATH=/opt/ros/noetic_static \ > -DCMAKE_BUILD_TYPE=Release \ > -DBoost_USE_STATIC_LIBS=ON Base path: /home/aurora/rs485_static_ws Source space: /home/aurora/rs485_static_ws/src Build space: /home/aurora/rs485_static_ws/build Devel space: /home/aurora/rs485_static_ws/devel Install space: /home/aurora/rs485_static_ws/install #### #### Running command: "cmake /home/aurora/rs485_static_ws/src -DCMAKE_PREFIX_PATH=/opt/ros/noetic_static -DCMAKE_BUILD_TYPE=Release -DBoost_USE_STATIC_LIBS=ON -DCATKIN_DEVEL_PREFIX=/home/aurora/rs485_static_ws/devel -DCMAKE_INSTALL_PREFIX=/home/aurora/rs485_static_ws/install -G Unix Makefiles" in "/home/aurora/rs485_static_ws/build" #### CMake Warning (dev) in CMakeLists.txt: No project() command is present. The top-level CMakeLists.txt file must contain a literal, direct call to the project() command. Add a line of code such as project(ProjectName) near the top of the file, but after cmake_minimum_required(). CMake is pretending there is a "project(Project)" command on the first line. This warning is for project developers. Use -Wno-dev to suppress it. -- The C compiler identification is GNU 9.4.0 -- The CXX compiler identification is GNU 9.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Using CATKIN_DEVEL_PREFIX: /home/aurora/rs485_static_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic_static -- This workspace overlays: /opt/ros/noetic_static -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Found PY_em: /usr/lib/python3/dist-packages/em.py -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/aurora/rs485_static_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Found Threads: TRUE -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 1 packages in topological order: -- ~~ - rs485_node -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'rs485_node' -- ==> add_subdirectory(rs485_node) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- Found Boost: /usr/lib/aarch64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found version "1.71.0") found components: system filesystem thread -- rs485_node: 3 messages, 0 services -- Configuring done -- Generating done -- Build files have been written to: /home/aurora/rs485_static_ws/build #### #### Running command: "make -j8 -l8" in "/home/aurora/rs485_static_ws/build" #### Scanning dependencies of target std_msgs_generate_messages_cpp Scanning dependencies of target _rs485_node_generate_messages_check_deps_DualRS485Command Scanning dependencies of target _rs485_node_generate_messages_check_deps_backangle Scanning dependencies of target std_msgs_generate_messages_lisp Scanning dependencies of target std_msgs_generate_messages_nodejs Scanning dependencies of target std_msgs_generate_messages_eus Scanning dependencies of target _rs485_node_generate_messages_check_deps_RS485Command Scanning dependencies of target std_msgs_generate_messages_py [ 0%] Built target std_msgs_generate_messages_cpp [ 0%] Built target std_msgs_generate_messages_lisp [ 0%] Built target std_msgs_generate_messages_nodejs [ 0%] Built target std_msgs_generate_messages_eus [ 0%] Built target std_msgs_generate_messages_py [ 0%] Built target _rs485_node_generate_messages_check_deps_backangle [ 0%] Built target _rs485_node_generate_messages_check_deps_DualRS485Command [ 0%] Built target _rs485_node_generate_messages_check_deps_RS485Command Scanning dependencies of target rs485_node_generate_messages_lisp Scanning dependencies of target rs485_node_generate_messages_nodejs Scanning dependencies of target rs485_node_generate_messages_py Scanning dependencies of target rs485_node_generate_messages_cpp Scanning dependencies of target rs485_node_generate_messages_eus [ 12%] Generating Javascript code from rs485_node/DualRS485Command.msg [ 12%] Generating Lisp code from rs485_node/RS485Command.msg [ 12%] Generating Lisp code from rs485_node/backangle.msg [ 16%] Generating Javascript code from rs485_node/RS485Command.msg [ 25%] Generating EusLisp code from rs485_node/RS485Command.msg [ 25%] Generating Lisp code from rs485_node/DualRS485Command.msg [ 29%] Generating Python from MSG rs485_node/RS485Command [ 33%] Generating C++ code from rs485_node/RS485Command.msg [ 41%] Generating Python from MSG rs485_node/DualRS485Command [ 41%] Generating Python from MSG rs485_node/backangle [ 45%] Generating Javascript code from rs485_node/backangle.msg [ 50%] Generating EusLisp code from rs485_node/DualRS485Command.msg [ 50%] Built target rs485_node_generate_messages_lisp [ 54%] Generating C++ code from rs485_node/DualRS485Command.msg [ 58%] Generating EusLisp code from rs485_node/backangle.msg [ 58%] Built target rs485_node_generate_messages_nodejs [ 62%] Generating EusLisp manifest code for rs485_node [ 66%] Generating C++ code from rs485_node/backangle.msg [ 70%] Generating Python msg __init__.py for rs485_node [ 70%] Built target rs485_node_generate_messages_cpp [ 70%] Built target rs485_node_generate_messages_eus [ 70%] Built target rs485_node_generate_messages_py Scanning dependencies of target rs485_node_generate_messages Scanning dependencies of target rs485_node_lib [ 70%] Built target rs485_node_generate_messages [ 75%] Building CXX object rs485_node/CMakeFiles/rs485_node_lib.dir/src/serial_port.cpp.o [ 79%] Building CXX object rs485_node/CMakeFiles/rs485_node_lib.dir/src/protocol_handler.cpp.o [ 83%] Linking CXX static library /home/aurora/rs485_static_ws/devel/lib/librs485_node_lib.a [ 83%] Built target rs485_node_lib Scanning dependencies of target angle_publisher_node Scanning dependencies of target rs485_node [ 91%] Building CXX object rs485_node/CMakeFiles/angle_publisher_node.dir/src/angle_publisher_node.cpp.o [ 91%] Building CXX object rs485_node/CMakeFiles/rs485_node.dir/src/rs485_node.cpp.o In file included from /opt/ros/noetic_static/include/ros/ros.h:40, from /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:1: /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp: In lambda function: /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:65:22: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 9 has type ‘rs485_node::backangle_<std::allocator<void> >::_circleAngle_type’ {aka ‘unsigned int’} [-Wformat=] 65 | ROS_INFO("电机id=%d, 其单圈圈角度读数为%ld", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66 | m.id, m.circleAngle); | ~~~~~~~~~~~~~ | | | rs485_node::backangle_<std::allocator<void> >::_circleAngle_type {aka unsigned int} /opt/ros/noetic_static/include/ros/console.h:351:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’ 351 | ole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__) | ^~~~~~~~~~~ /opt/ros/noetic_static/include/ros/console.h:390:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’ 390 | ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \ | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /opt/ros/noetic_static/include/ros/console.h:575:35: note: in expansion of macro ‘ROS_LOG_COND’ 575 | #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__) | ^~~~~~~~~~~~ /opt/ros/noetic_static/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’ 110 | #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__) | ^~~~~~~ /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:65:13: note: in expansion of macro ‘ROS_INFO’ 65 | ROS_INFO("电机id=%d, 其单圈圈角度读数为%ld", | ^~~~~~~~ /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:65:65: note: format string is defined here 65 | ROS_INFO("电机id=%d, 其单圈圈角度读数为%ld", | ~~^ | | | long int | %d [ 95%] Linking CXX executable /home/aurora/rs485_static_ws/devel/lib/rs485_node/rs485_node [100%] Linking CXX executable /home/aurora/rs485_static_ws/devel/lib/rs485_node/angle_publisher_node /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(pthread_create.o): in function `allocate_stack': /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:525: undefined reference to `_dl_stack_flags' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:525: undefined reference to `_dl_stack_flags' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:647: undefined reference to `_dl_stack_flags' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:647: undefined reference to `_dl_stack_flags' /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(nptl-init.o): in function `__pthread_initialize_minimal_internal': /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:335: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:335: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:344: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:360: undefined reference to `_dl_init_static_tls' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:362: undefined reference to `_dl_wait_lookup_done' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:360: undefined reference to `_dl_init_static_tls' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:362: undefined reference to `_dl_wait_lookup_done' /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(nptl-init.o): in function `__pthread_get_minstack': /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:393: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:393: undefined reference to `_dl_pagesize' /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(unwind.o): in function `_jmpbuf_sp': /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' collect2: error: ld returned 1 exit status make[2]: *** [rs485_node/CMakeFiles/rs485_node.dir/build.make:111: /home/aurora/rs485_static_ws/devel/lib/rs485_node/rs485_node] Error 1 make[1]: *** [CMakeFiles/Makefile2:442: rs485_node/CMakeFiles/rs485_node.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(unwind.o): in function `_jmpbuf_sp': /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' collect2: error: ld returned 1 exit status make[2]: *** [rs485_node/CMakeFiles/angle_publisher_node.dir/build.make:110: /home/aurora/rs485_static_ws/devel/lib/rs485_node/angle_publisher_node] Error 1 make[1]: *** [CMakeFiles/Makefile2:855: rs485_node/CMakeFiles/angle_publisher_node.dir/all] Error 2 make: *** [Makefile:141: all] Error 2 Invoking "make -j8 -l8" failed
最新发布
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值