ES6 变量声明,箭头函数,数组方法,解构赋值,JSON,类与继承,模块化练习

let和const变量声明

var 可以重复声明,变量提升,没有块作用域 {}
let, const 不可以重复声明,有块作用域
const 声明时必须赋值,值不可修改,声明的复合类型(对象和数组)指向内存地址

  1. // let和const变量声明
  2. var a = 'a';
  3. let b = 'b';
  4. const c = ['c'];
  5. // 块作用域
  6. if (true) {
  7. // 无块作用域
  8. var a = 'aa';
  9. // 有块作用域
  10. let b = 'bb';
  11. const c = ['cc'];
  12. }
  13. // 复合类型指向指针地址
  14. c[0] = 'ccc';
  15. // aa b ['ccc']
  16. console.log(a, b, c);

箭头函数,数组 filter,map,reduce 方法

  1. // 箭头函数,数组 filter,map,reduce 方法
  2. const evenPow2 = (...param) => param.filter(el => el % 2 === 0).map(el => Math.pow(el, 2)).reduce((p, n) => p + n, 0);
  3. // 计算一组值中偶数平方的和,输出 20
  4. console.log(evenPow2(1, 2, 3, 4, 5));
  5. // filter 数组去重
  6. const arr = ['a', 1, 'b', 'a', 'b', 1, 2];
  7. let arrUnique = arr.filter((value, i, arr) => arr.indexOf(value) === i);
  8. // ['a', 1, 'b', 2]
  9. console.log(arrUnique);
  10. // reduce 数组去重
  11. arrUnique = arr.reduce((prev, curr) => prev.includes(curr) ? prev : [...prev, curr], []);
  12. // ['a', 1, 'b', 2]
  13. console.log(arrUnique);

解构赋值

  1. // 解构赋值
  2. const [{x,z}, [xy, yz, zx]] = [{x:1, y:2, z:3}, [12, 23, 31]];
  3. // 1 2 31
  4. console.log(x, z, zx);

类与继承和JSON串行化与反串行化

  1. // 类与继承和JSON串行化与反串行化
  2. class Person {
  3. // 构造方法
  4. constructor(name, sex) {
  5. this.name = name;
  6. this.sex = sex;
  7. // this.say = () => `My name is ${this.name}`;
  8. }
  9. // say = function () {
  10. // return `My name is ${this.name}`;
  11. // }
  12. // say = () => `My name is ${this.name}`;
  13. say() {
  14. return `My name is ${this.name}`;
  15. }
  16. }
  17. class Student extends Person {
  18. constructor(name, sex, school) {
  19. // 父类构造函数,新建父类的 this 对象
  20. super(name, sex);
  21. this.school = school;
  22. }
  23. }
  24. const student1 = new Student('tesName', 'male', 'php.cn');
  25. // tesName male php.cn My name is tesName
  26. console.log(student1.name, student1.sex, student1.school, student1.say());
  27. // 串行化
  28. const strStudent1 = JSON.stringify(student1);
  29. // 只能串行化 `字符串,数字,对象,数组,布尔,null` 因此 say() 函数被忽略
  30. // {"name":"tesName","sex":"male","school":"php.cn"}
  31. console.log(strStudent1);
  32. // 反串行化,返回 {name: 'tesName', sex: 'male', school: 'php.cn'}
  33. parseStudent1 = JSON.parse(strStudent1);
  34. console.log(parseStudent1);

es6练习

module 模块化编程

  • demo2.js

    1. const a = 1;
    2. const show = () => 'hello world!';
    3. const test = () => 'test';
    4. export {a as aVar, show, test as default};
  • demo.js

    1. // 部分导入
    2. import {aVar as aTestVar, show} from "./demo2.js";
    3. // 1 'hello world!'
    4. console.log(aTestVar, show());
    5. // 缺省导入
    6. import testFunc from "./demo2.js";
    7. // 1 'hello world!' 'test'
    8. console.log(testFunc());
    9. // 全部导入
    10. import * as obj from "./demo2.js";
    11. // 1 'hello world!' 'test'
    12. console.log(obj.aVar, obj.show(), obj.default());
  • demo.html

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script src="demo.js" type="module"></script>
    7. </head>
    8. <body>
    9. </body>
    10. </html>

模块化编程

更多相关文章

  1. 箭头函数的基础使用
  2. android“设置”里的版本号
  3. Android中文API(144) —— JsonWriter
  4. Android系统下如何在程序中对XML里面元素进行赋值
  5. android sqlite 图片保存和读出
  6. Android评论留言页面
  7. [Android] ListView 结合SimpleAdapter使用
  8. Android(安卓)Http通信(使用 标准Java接口)及解析Json
  9. Android(安卓)图片转成String保存

随机推荐

  1. use在php中的使用方法(代码示例)
  2. PHP高级应用的讲解
  3. php的定界符<<<EOF的解析
  4. PHP Composer是什么技术?一起看看
  5. Mac搭建php的开发环境(图文详解)
  6. php获取mp3音频信息实例教程
  7. utf8和utf8mb4的区别详解
  8. 一看就会的php网站源码安装简易教程
  9. 编程小白学习php必须要了解的事
  10. PHP微信公众号支付教程(含图文)