ES6新特性01-let关键字
文章目录
- ES6新特性01-let关键字
- 一、let关键字
- 二、let实践案例
一、let关键字
- 变量不能重复声明
- 块儿级作用域 全局, 函数, eval
- 不存在变量提升
- 不影响作用域链
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>let</title> </head> <body> <script> //声明变量 let a; let b,c,d; let e = 100; let f = 521, g = 'iloveyou', h = []; //1. 变量不能重复声明 // let star = '罗志祥'; // let star = '小猪'; //2. 块儿级作用域 全局, 函数, eval // if else while for // { // let girl = '周扬青'; // } // console.log(girl); //3. 不存在变量提升 // console.log(song); // let song = '恋爱达人'; //4. 不影响作用域链 { let school = '尚硅谷'; function fn(){ console.log(school); } fn(); } </script> </body> </html>
二、let实践案例
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>点击 DIV 换色</title> <link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style> .item { width: 100px; height: 50px; border: solid 1px rgb(42, 156, 156); float: left; margin-right: 10px; } </style> </head> <body> <div class="container"> <h2 class="page-header">点击切换颜色</h2> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> <script> //获取div元素对象 let items = document.getElementsByClassName('item'); //遍历并绑定事件 for(let i = 0;i<items.length;i++){ items[i].onclick = function(){ //修改当前元素的背景颜色 // this.style.background = 'pink'; items[i].style.background = 'pink'; } } </script> </body> </html> s[i].style.background = 'pink'; } } </script> </body> </html>
最后
以上就是清新百合最近收集整理的关于ES6新特性01-let关键字ES6新特性01-let关键字的全部内容,更多相关ES6新特性01-let关键字ES6新特性01-let关键字内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复