作用域(scope)规定了变量能够被访问的“范围”,离开了这个“范围”变量便不能被访问。
局部作用域可分为函数作用域和块作用域。
在函数内部声明的变量只能在函数内部被访问,外部无法直接访问。

总结:
在JavaScript中使用{}包裹的代码称为代码块,代码块内部声明的变量外部将 {有可能} 无法被访问。


变量的数量大于单元值数量时,多余的变量将被赋值为undefined


剩余参数返回的还是一个数组

允许初始化变量的默认值,且只有单元值为undefined时默认值才会生效



对象解构是将对象属性和方法快速批量赋值给一系列变量的简洁语法

可以从一个对象中提取变量并同时修改新的变量名

对象解构的变量名 可以重新改名 旧变量名:新变量名


需求①:有个对象:const pig = {name:'佩奇',age:6}
结构为变量:完成对象解构,并以此打印出值
需求②:请将pig对象中的name,通过对象解构的形式改为uname,并打印输出
需求③:请将数组对象,完成 商品名和价格的解构
-
- //①
- // const pig = {name:'佩奇',age:6}
- // const {name,age} = pig
- // console.log(name,age)
- //②
- const pig = [{name:'佩奇',age:6}]
- const [{name : uname,age}] = pig
- console.log(uname,age)
- // ③
- const goods = [{goodsName:'小米',price:1999}]
- const [{goodsName,price}] = goods
- console.log(goodsName,price)
-
-





将数据完成3个需求


- DOCTYPE html>
-
-
-
-
-
Document -
-
- // 1. 这是后台传递过来的数据
- const msg = {
- "code": 200,
- "msg": "获取新闻列表成功",
- "data": [
- {
- "id": 1,
- "title": "5G商用自己,三大运用商收入下降",
- "count": 58
- },
- {
- "id": 2,
- "title": "国际媒体头条速览",
- "count": 56
- },
- {
- "id": 3,
- "title": "乌克兰和俄罗斯持续冲突",
- "count": 1669
- },
-
- ]
- }
-
- // 需求1: 请将以上msg对象 采用对象解构的方式 只选出 data 方面后面使用渲染页面
- const {data} = msg
- console.log(data)
-
- // 需求2: 上面msg是后台传递过来的数据,我们需要把data选出当做参数传递给 函数
-
- function render({data}) {
- // 我们只要 data 数据
- // 内部处理
- console.log(data)
- }
- render(msg)
-
- // 需求3, 为了防止msg里面的data名字混淆,要求渲染函数里面的数据名改为 myData
- function render({data:myData}) {
- // 要求将 获取过来的 data数据 更名为 myData
- // 内部处理
- console.log(myData)
- }
- render(msg)
-
-
-
语法:

例如:

注意:
核心思路:有多少条数据,就渲染多少模块,然后生成对应的html结构标签,赋值给list标签即可
- 利用forEach遍历数据里面的数据
- 拿到数据,利用字符串拼接生成结构添加到页面中
- 注意:传递参数的时候,可以使用对象解构
- DOCTYPE html>
-
-
-
-
-
商品渲染 -
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
-
- .list {
- width: 990px;
- margin: 0 auto;
- display: flex;
- flex-wrap: wrap;
- padding-top: 100px;
- }
-
- .item {
- width: 240px;
- margin-left: 10px;
- padding: 20px 30px;
- transition: all .5s;
- margin-bottom: 20px;
- }
-
- .item:nth-child(4n) {
- margin-left: 0;
- }
-
- .item:hover {
- box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
- transform: translate3d(0, -4px, 0);
- cursor: pointer;
- }
-
- .item img {
- width: 100%;
- }
-
- .item .name {
- font-size: 18px;
- margin-bottom: 10px;
- color: #666;
- }
-
- .item .price {
- font-size: 22px;
- color: firebrick;
- }
-
- .item .price::before {
- content: "¥";
- font-size: 14px;
- }
-
-
-
-
-
-
- const goodsList = [
- {
- id: '4001172',
- name: '称心如意手摇咖啡磨豆机咖啡豆研磨机',
- price: '289.00',
- picture: 'https://yanxuan-item.nosdn.127.net/84a59ff9c58a77032564e61f716846d6.jpg',
- },
- {
- id: '4001594',
- name: '日式黑陶功夫茶组双侧把茶具礼盒装',
- price: '288.00',
- picture: 'https://yanxuan-item.nosdn.127.net/3346b7b92f9563c7a7e24c7ead883f18.jpg',
- },
- {
- id: '4001009',
- name: '竹制干泡茶盘正方形沥水茶台品茶盘',
- price: '109.00',
- picture: 'https://yanxuan-item.nosdn.127.net/2d942d6bc94f1e230763e1a5a3b379e1.png',
- },
- {
- id: '4001874',
- name: '古法温酒汝瓷酒具套装白酒杯莲花温酒器',
- price: '488.00',
- picture: 'https://yanxuan-item.nosdn.127.net/44e51622800e4fceb6bee8e616da85fd.png',
- },
- {
- id: '4001649',
- name: '大师监制龙泉青瓷茶叶罐',
- price: '139.00',
- picture: 'https://yanxuan-item.nosdn.127.net/4356c9fc150753775fe56b465314f1eb.png',
- },
- {
- id: '3997185',
- name: '与众不同的口感汝瓷白酒杯套组1壶4杯',
- price: '108.00',
- picture: 'https://yanxuan-item.nosdn.127.net/8e21c794dfd3a4e8573273ddae50bce2.jpg',
- },
- {
- id: '3997403',
- name: '手工吹制更厚实白酒杯壶套装6壶6杯',
- price: '99.00',
- picture: 'https://yanxuan-item.nosdn.127.net/af2371a65f60bce152a61fc22745ff3f.jpg',
- },
- {
- id: '3998274',
- name: '德国百年工艺高端水晶玻璃红酒杯2支装',
- price: '139.00',
- picture: 'https://yanxuan-item.nosdn.127.net/8896b897b3ec6639bbd1134d66b9715c.jpg',
- },
- ]
-
- // 1.声明一个字符串变量
- let str = ''
- // 2. 遍历数据
- goodsList.forEach(item =>{
- // console.log(item) // 可以得到每一个数组元素 对象
- // const {id} = item
- const {name,price,picture} = item
- str += `
-
-

-
${name}
-
${price}
-
- `
- })
- document.querySelector('.list').innerHTML = str
-
-
filter()方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素
主要使用场景:筛选数组符合条件的元素,并返回筛选之后元素的新数组
语法:

参数:currentValue必须写,index可选
因为返回新数组,所以不会影响原数组
例如:
需求:
- 渲染数据列表 分析:
- 根据filter选择不同条件显示不同商品
- 渲染页面利用forEach遍历数据里面的数据,并渲染数据列表
商品列表价格筛选
- 根据选择不同条件显示不同商品
- 点击采取事件委托方式.filter
- 利用过滤函数filter筛选出符合条件的数据,因为生成的是一个数组,传递给渲染函数即可
- 筛选条件是根据点击的data-index来判断
- 可以使用对象解构,把事件对象解构
- 因为全部区间不需要筛选,直接把goodList渲染即可
- DOCTYPE html>
-
-
-
-
-
商品渲染 -
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
-
- .list {
- width: 990px;
- margin: 0 auto;
- display: flex;
- flex-wrap: wrap;
- }
-
- .item {
- width: 240px;
- margin-left: 10px;
- padding: 20px 30px;
- transition: all .5s;
- margin-bottom: 20px;
- }
-
- .item:nth-child(4n) {
- margin-left: 0;
- }
-
- .item:hover {
- box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
- transform: translate3d(0, -4px, 0);
- cursor: pointer;
- }
-
- .item img {
- width: 100%;
- }
-
- .item .name {
- font-size: 18px;
- margin-bottom: 10px;
- color: #666;
- }
-
- .item .price {
- font-size: 22px;
- color: firebrick;
- }
-
- .item .price::before {
- content: "¥";
- font-size: 14px;
- }
-
- .filter {
- display: flex;
- width: 990px;
- margin: 0 auto;
- padding: 50px 30px;
- }
-
- .filter a {
- padding: 10px 20px;
- background: #f5f5f5;
- color: #666;
- text-decoration: none;
- margin-right: 20px;
- }
-
- .filter a:active,
- .filter a:focus {
- background: #05943c;
- color: #fff;
- }
-
-
-
-
-
-
- // 2. 初始化数据
- const goodsList = [
- {
- id: '4001172',
- name: '称心如意手摇咖啡磨豆机咖啡豆研磨机',
- price: '289.00',
- picture: 'https://yanxuan-item.nosdn.127.net/84a59ff9c58a77032564e61f716846d6.jpg',
- },
- {
- id: '4001594',
- name: '日式黑陶功夫茶组双侧把茶具礼盒装',
- price: '288.00',
- picture: 'https://yanxuan-item.nosdn.127.net/3346b7b92f9563c7a7e24c7ead883f18.jpg',
- },
- {
- id: '4001009',
- name: '竹制干泡茶盘正方形沥水茶台品茶盘',
- price: '109.00',
- picture: 'https://yanxuan-item.nosdn.127.net/2d942d6bc94f1e230763e1a5a3b379e1.png',
- },
- {
- id: '4001874',
- name: '古法温酒汝瓷酒具套装白酒杯莲花温酒器',
- price: '488.00',
- picture: 'https://yanxuan-item.nosdn.127.net/44e51622800e4fceb6bee8e616da85fd.png',
- },
- {
- id: '4001649',
- name: '大师监制龙泉青瓷茶叶罐',
- price: '139.00',
- picture: 'https://yanxuan-item.nosdn.127.net/4356c9fc150753775fe56b465314f1eb.png',
- },
- {
- id: '3997185',
- name: '与众不同的口感汝瓷白酒杯套组1壶4杯',
- price: '108.00',
- picture: 'https://yanxuan-item.nosdn.127.net/8e21c794dfd3a4e8573273ddae50bce2.jpg',
- },
- {
- id: '3997403',
- name: '手工吹制更厚实白酒杯壶套装6壶6杯',
- price: '99.00',
- picture: 'https://yanxuan-item.nosdn.127.net/af2371a65f60bce152a61fc22745ff3f.jpg',
- },
- {
- id: '3998274',
- name: '德国百年工艺高端水晶玻璃红酒杯2支装',
- price: '139.00',
- picture: 'https://yanxuan-item.nosdn.127.net/8896b897b3ec6639bbd1134d66b9715c.jpg',
- },
- ]
-
-
- // 1. 渲染函数 封装
- function render(arr){
- // 声明空字符串
- let str = ''
- // 遍历数组
- arr.forEach(item =>{
- // 解构
- const {name,picture,price} = item
- str += `
-
-

-
${name}
-
${price}
-
- `
- })
- // 追加给list
- document.querySelector('.list').innerHTML = str
- }
- render(goodsList) // 页面一打开就需要渲染
-
- // 2. 过滤筛选
- document.querySelector('.filter').addEventListener('click',e =>{
- // e.target.dataset.id
- const {tagName,dataset} = e.target
- // 判断
- if(e.target.tagName === 'A'){
- // console.log(11)
- // arr返回的新数组
- let arr = goodsList
- if(dataset.index === '1'){
- arr = goodsList.filter(item => item.price > 0 && item.price <= 100)
- } else if(dataset.index === '2'){
- arr = goodsList.filter(item => item.price >= 100 && item.price <= 300)
- } else if(dataset.index === '3'){
- arr = goodsList.filter(item => item.price >= 300)
- }
- // 渲染函数
- render(arr)
- }
- })
-
-