• 七天入门node.js(02)


    es6面向对象

    什么是面向对象

    机器语言->汇编语言->低级语言(面向过程)->高级语言(面向对象)->框架->api

    面向对象 : 把一些公共的东西封装起来 , 通过new出来的对象直接调用就行

    面向对象的知识点-call

    类和构造函数

    改变this指向

    call 是什么

    1. function fn(...args) {
    2. console.log(this, args);
    3. }
    4. /*
    5. * fn 放在对象中 对象.fn()可以调用这个方法 此时this指向该对象
    6. * */
    7. /*
    8. * 改变this指向 js提供三个方法 call apply bind
    9. * 函数.call(参数1:想改变成的this, 后面的参数 会依次传递到原函数中)
    10. * 函数.apply(参数1:想改变成的this, 第二个参数是一个数组)
    11. * 函数.apply(参数1:想改变成的this,后面的参数 会依次传递到原函数中)
    12. 特点: 不会立即执行, 会返回一个新的函数 执行这个函数就可以传递
    13. *
    14. * */
    15. let arr = [1, 2, 3];
    16. fn.call(arr, '小耿', '小杨', '小候',);

    call的应用-类型判断

    1. /*
    2. * 所以 对于类型检测 最好采用下面方式:
    3. * */
    4. const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target);
    5. const isArray = isType("Array");
    6. const isObject = isType("Object");
    7. const isRegExp = isType("RegExp");
    8. const isNull = isType("Null");
    9. console.log(isArray({}));

    apply

    1. function fn1(...args) {
    2. console.log(this, args);
    3. }
    4. let obj = {name: '张三'};
    5. //call 传参可以传无限个 apply 传参用数组的方式传递
    6. fn1.apply(obj, ['1', '2','fsfs']);

    bind

    1. function fn(...args) {
    2. console.log(this,args);//['hello',{name: 'name'},'1,2,3']
    3. }
    4. let arr = [1, 2, 3];
    5. //bind也可以改变this指向 但是不会立即执行
    6. //此时 bind返回一个新的函数
    7. //如果想传参的话 通过fn.bind 或者 返回的新的函数中传参
    8. const newFn = fn.bind(arr, 'hello',{name: 'name'});
    9. newFn('1,2,3');

    面向对象的知识点-prototype

    prototype的本质

    对象.__proto__ === 类.prototype  解释过来就是 类可以调用的属性或者方法都在对象的原型上
    

    es6中面向对象

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <script>
    9. /*
    10. * es5中的面向对象
    11. *
    12. * */
    13. let arr = new Array(1, 2, 3);
    14. //1: 对象(实例) = new 类/构造函数()
    15. //Person 大写
    16. //Person 类 === 构造函数
    17. //es中 类 === 构造函数
    18. //类: 只要有 new new后面跟着的就是类
    19. //构造函数 : 通过参数 可以传递到函数本身的this身上
    20. function Person(name,age) {
    21. //this === person // true
    22. this.name = name;
    23. this.age = age;
    24. this.fn = function () {
    25. console.log(this === person);
    26. }
    27. }
    28. //new 得到了实例
    29. let person = new Person('张三',20);//对象
    30. console.log(person.name);
    31. console.log(person.age);
    32. console.log(person.fn());
    33. </script>
    34. </body>
    35. </html>

    super : 父类的原型

    1. class Person {
    2. constructor(name) {
    3. this.name = name;
    4. console.log(this.name);
    5. }
    6. getName() {
    7. console.log('父类的getName');
    8. }
    9. }
    10. class Man extends Person {
    11. constructor(props) {
    12. super(props);
    13. super.getName();//调用父类的getName()
    14. }
    15. }

    es6实现完整的继承

    1. class Person {
    2. constructor(name) {
    3. this.name = name;
    4. console.log(this.name);
    5. }
    6. getName() {
    7. console.log('父类的getName');
    8. }
    9. }
    10. class Man extends Person {
    11. constructor(props) {
    12. super(props);
    13. }
    14. getName() {
    15. console.log('子类的getName')
    16. }
    17. getSuperName() {
    18. super.getName();
    19. console.log(Person.prototype.getName === super.getName);
    20. }
    21. }
    22. new Man('小明').getSuperName();

    面向对象-小球案例

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <style>
    7. .circle {
    8. /*width: 40px;*/
    9. /*height: 40px;*/
    10. border-radius: 50%;
    11. /*background-color: #d14596;*/
    12. position: absolute;
    13. }
    14. .rect {
    15. position: absolute;
    16. }
    17. </style>
    18. </head>
    19. <body>
    20. <div id="box">
    21. <button id="btnCircle">随机生成小球</button>
    22. <button id="btnRect">随机生成方块</button>
    23. </div>
    24. <script>
    25. btnCircle.onclick = function () {
    26. // 面向函数
    27. // let oCircle = document.createElement('div')
    28. // oCircle.classList.add('circle');
    29. // box.appendChild(oCircle);
    30. // 面向对象
    31. // 创建一个对象 class
    32. new Circle();
    33. };
    34. btnRect.onclick = function () {
    35. new Rect();
    36. };
    37. class Graph{
    38. random(min, max) {
    39. return Math.floor(Math.random() * (max - min) + min);
    40. }
    41. randomColor() {
    42. //# 1-9 + abcdef
    43. //#1efc4d
    44. const letters = ['a','b','c','d','e','f'];
    45. const strs = [...letters];
    46. for (let i = 0; i < 10; i++) {
    47. strs.push(i + '');
    48. }
    49. // console.log(strs);
    50. //#1efc4d
    51. let i = 0;
    52. let colorStr = '#';
    53. while (i < 6) {
    54. //随机的下标
    55. let index = this.random(0, strs.length);
    56. colorStr += strs[index];
    57. i++;
    58. }
    59. // console.log(colorStr);
    60. return colorStr;
    61. }
    62. }
    63. class Circle extends Graph{
    64. /*
    65. * 构造函数可以省略
    66. *
    67. * 小球:属性 : 大小 小球的颜色 位置
    68. * 方法 : 功能 创建小球/随机数/随机颜色/拖拽
    69. *
    70. * */
    71. constructor() {
    72. /*
    73. * 属性 : 小球本身 颜色 位置
    74. * */
    75. /*大小*/
    76. super();
    77. this.r = this.random(10,100);
    78. this.x = this.random(10,1200);
    79. this.y = this.random(10,500);
    80. this.color = this.randomColor();
    81. //调用创建小球的方法
    82. this.createEl();
    83. }
    84. /*
    85. * 方法 : 功能
    86. * */
    87. /*创建小球*/
    88. createEl() {
    89. let {r, color, x, y} = this;
    90. this.circle = document.createElement('div');
    91. this.circle.classList.add('circle');
    92. this.circle.style.width = `${r * 2}px`;
    93. this.circle.style.height = `${r * 2}px`;
    94. this.circle.style.backgroundColor = `${color}`;
    95. this.circle.style.left = `${x}px`;
    96. this.circle.style.top = `${y}px`;
    97. box.appendChild(this.circle);
    98. }
    99. }
    100. class Rect extends Graph{
    101. /*
    102. * 构造函数可以省略
    103. *
    104. * 小球:属性 : 大小 小球的颜色 位置
    105. * 方法 : 功能 创建小球/随机数/随机颜色/拖拽
    106. *
    107. * */
    108. constructor() {
    109. super();
    110. /*
    111. * 属性 : 小球本身 颜色 位置
    112. * */
    113. /*大小*/
    114. this.r = this.random(10,100);
    115. this.x = this.random(10,1200);
    116. this.y = this.random(10,500);
    117. this.color = this.randomColor();
    118. //调用创建小球的方法
    119. this.createEl();
    120. this.drag();
    121. }
    122. /*
    123. * 方法 : 功能
    124. * */
    125. /*创建小球*/
    126. createEl() {
    127. let {r, color, x, y} = this;
    128. this.circle = document.createElement('div');
    129. this.circle.classList.add('rect');
    130. this.circle.style.width = `${r * 2}px`;
    131. this.circle.style.height = `${r * 2}px`;
    132. this.circle.style.backgroundColor = `${color}`;
    133. this.circle.style.left = `${x}px`;
    134. this.circle.style.top = `${y}px`;
    135. this.circle.style.zIndex = '0';
    136. box.appendChild(this.circle);
    137. }
    138. random(min, max) {
    139. return Math.floor(Math.random() * (max - min) + min);
    140. }
    141. drag() {
    142. let {circle} = this;
    143. let {zIndex} = circle.style;
    144. circle.onmousedown = function (ev) {
    145. // console.log(ev);
    146. document.onmousemove = function (event) {
    147. console.log(event);
    148. };
    149. document.onmouseup = function () {
    150. }
    151. }
    152. }
    153. }
    154. </script>
    155. </body>
    156. </html>

    es6 中数组常用方法

    静态方法

    Array.from(): 将一个伪数组转换成数组

    1. /*
    2. * 数组的静态方法
    3. * Array.from 将一个非数组-->数组 只能将伪数组变成数组
    4. * Array.isArray 判断一个变量是不是数组
    5. * */
    6. /*
    7. * 将一个非数组--》数组 Array.prototype 成员方法 Array.方法 静态方法
    8. * Array.isArray() : 只能将伪数组变成数组 [下标]
    9. * */
    10. let newArr = Array.from('abc');
    11. console.log(newArr);
    12. console.log(Array.from(document.getElementsByTagName('li')));
    13. console.log(Array.from(123));
    14. // console.log(Array.from(undefined));
    15. // console.log(Array.from(null));
    16. console.log(Array.from({a:1,b:2}));
    17. console.log(Array.from([1,2,3]));

    Array.is(): 判断是不是一个数组

    1. //判断一个变量是不是数组
    2. //typeof arr // object 这种局限性在于 引用类型通过typeof都会识别成 object
    3. //这种方式就是专门用来判断是不是数组的
    4. console.log(Array.isArray({}));

    成员方法

    这里面咱们主要是提出es6中给我们提供的一些数组的常用方法

    我们首先回顾es5中有哪些常用方法

    push / pop / shift / unshift / reserve / sort/ join/ concat
    

    回顾一下还记得其中的用法吗

    es6中数组新增常用的方法

    • arr.forEach()

    • arr.map()

    • arr.every()

    • arr.filter()

    • arr.reduce()

    • arr.some()

    • arr.find()

    • arr.findIndex()

      这些方法的共同特点是参数都是回调函数其中除了reduce之外别剩余的参数的回调函数都(item,index,arr)=>{} 其中 item为数组每一项 index为数组下标 arr为当前数组

      接下来我们一个一个看

    forEach

    1. /*
    2. * arr.forEach((item可写可不写,index可写可不写,arr可写可不写)=>{})
    3. *
    4. * 特点: 不能打断 比如 break continue 都不能用
    5. * return 也不好使
    6. * */
    7. let arr = [1, 2, 3];
    8. arr.forEach((item,index,arr)=>{
    9. //item : 数组中的每一个元素
    10. //index: 下标
    11. //arr : 数组本身
    12. console.log(item, index, arr);
    13. // if (index === 1) {
    14. // return;
    15. // }
    16. })
    17. let arr = [1, 2, 3, 4];
    18. /*
    19. * 参数 : 函数 每一个我们想要的参数 都在函数的参数里面
    20. * 没有返回值
    21. * */
    22. let newArr = arr.forEach((item, index, arr) => {
    23. console.log(item);
    24. });
    25. console.log(newArr);

    map

    1. let arr2 = [{name: '小候', age: 11},
    2. {name: '小靳', age: 12},
    3. {name: '小闫', age: 13}, {name: '小杨', age: 14}, {
    4. name: '小袁',
    5. age: 21
    6. }, {name: '小赵', age: 22}, {name: '小兴', age: 23}, {name: '小康', age: 24}, {name: '小邵', age: 24}];
    7. /*
    8. * map:遍历
    9. * 参数 : 是一个函数 每一个我们想要的参数 都在函数的参数里面
    10. * 返回值 新的数组 newArray每次push的值 是map里面的返回值
    11. * */
    12. newArray = arr2.map((item, index, arr) => {
    13. return ++item.age + 50;
    14. });
    15. console.log(newArray);

    some

    1. /*
    2. * some: 只要回调函数有某一个返回条件满足 就返回true
    3. * */
    4. flag = arr2.some(item => item.age > 20);
    5. console.log(flag);

    filter

    1. /*
    2. * filter : 机制和forEach 作用是:过滤
    3. * 返回值 : 返回新的数组 回调函数中返回的条件决定新的数组的元素
    4. * */
    5. newArr = arr2.filter((item, index) => {
    6. //条件一旦满足 newArr就会push当前item
    7. return item.age > 20;
    8. });
    9. console.log(newArr);
    10. console.log(`可以看电影的成员有:`);
    11. newArr.forEach(item => console.log(`${item.name}`));

    reduce

    1. /*
    2. * reduce 面试常客
    3. *
    4. * 参数介绍:
    5. * prev: 如果第一次循环 prev是数组里面的第一个元素
    6. * 从第二次开始循环 prev是上一次回调函数的返回值
    7. *
    8. * next: 始终代表 下一次循环的元素
    9. *
    10. * */
    11. [1, 2, 3, 4, 5].reduce((prev, next) => {
    12. //1 , 2 第一次
    13. //3 , 3 第二次
    14. //6 , 4 第三次
    15. console.log(prev, next);
    16. return prev + next;
    17. });
    18. /*
    19. * 常用 forEach map filter some
    20. * 不太常用 : find every reduce findIndex
    21. * */

    不常用方法

    • find
    • findIndex
    • every

    find和findIndex

    1. /*
    2. * find:机制 forEach
    3. * 从数组中查找到符合条件的元素, 返回一个我们查找到的元素在回调函数中返回我们查找的条件的第一个成员
    4. * */
    5. arr2 = [{name: '小候', age: 11},
    6. {name: '小靳', age: 12},
    7. {name: '小闫', age: 13}, {name: '小杨', age: 14}, {
    8. name: '小袁',
    9. age: 21
    10. }, {name: '小赵', age: 22}, {name: '小兴', age: 23}, {name: '小康', age: 24}, {name: '小邵', age: 24}];
    11. let has = arr2.find(item => {
    12. return item.age > 20;
    13. });
    14. // console.log(num);
    15. if (has) {//元素 / undefined
    16. console.log('终于找到你,还好我没放弃');
    17. } else {
    18. console.log('终于我们还是擦肩儿过');
    19. }
    20. let index = arr2.findIndex(item => item.age === 11);
    21. //如果没找到 返回-1 如果找到了 返回当前数组的下标
    22. console.log('findIndex', index);

    every

    1. /*
    2. * every : 每一个都符合条件 只要有一个不符合条件 返回false
    3. * */
    4. let flag = arr2.every((item, index) => {
    5. // return typeof item.age === 'number';
    6. return item.age > 20;
    7. });
    8. console.log(flag);
  • 相关阅读:
    我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
    内网穿透工具frp使用入门
    1164 Good in C – PAT甲级真题
    java题库——认证考试题1
    英语语法 - 宾语从句
    大家好这是一首诗
    Proteus仿真--1602LCD显示仿手机键盘按键字符(仿真文件+程序)
    Springboot健身房管理系统毕业设计源码031807
    scrapy框架搭建
    NFNet:NF-ResNet的延伸,不用BN的4096超大batch size训练 | 21年论文
  • 原文地址:https://blog.csdn.net/weixin_45311714/article/details/127649746