AI智能
改变未来

js全局变量在去局部域内被改变,在全局域内的其所有值都随之改变

<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Document</title><style>.box1 {/* float: left; *//* float: right; */width: 100px;height: 100px;background-color: pink;}.box2 {/* float: left; */float: right;width: 100px;height: 50px;background-color: yellow;}.box3 {width: 300px;height: 300px;background-color: skyblue;}</style></head><body><div class=\"box1\">父盒子<div class=\"box2\">子盒子</div></div><div class=\"box3\"></div><script>var box1 = document.querySelector(\'.box1\');var box2 = document.querySelector(\'.box2\');var box3 = document.querySelector(\'.box3\');box2.addEventListener(\'click\', function() {num = 1;});var num = 0;// 全局变量在局部域内被改变,则在全局范围内也随之改变,即全局范围内的其他局部域里的全局变量也同步改变box1.addEventListener(\'click\', function() {console.log(num);});box3.addEventListener(\'click\', function() {console.log(num);});</script></body></html>

全局变量在局部域内被改变,则在全局范围内也随之改变,即全局范围内的其他局部域里的该全局变量也同步改变

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » js全局变量在去局部域内被改变,在全局域内的其所有值都随之改变