1. const canvas = document.querySelector('#draw');
    2. const c = canvas.getContext('2d');
    3. canvas.width = window.innerWidth;
    4. canvas.height = window.innerHeight;
    5. c.lineJoin = 'round';
    6. c.lineCap = 'round';
    7. c.lineWidth = 100;
    8. let isDrawing = false;
    9. let lastX = 0;
    10. let lastY = 0;
    11. let hue = 0;
    12. function draw(e) {
    13. if (!isDrawing) return;
    14. c.strokeStyle = `hsl(${hue}, 50%, 50%)`;
    15. c.beginPath();
    16. c.moveTo(lastX, lastY);
    17. c.lineTo(e.offsetX, e.offsetY);
    18. c.stroke();
    19. [lastX, lastY] = [e.offsetX, e.offsetY];
    20. hue++;
    21. }
    22. canvas.addEventListener('mousemove', draw);
    23. canvas.addEventListener('mousedown', (e) => {
    24. [lastX, lastY] = [e.offsetX, e.offsetY];
    25. isDrawing = true
    26. });
    27. canvas.addEventListener('mouseup', () => {isDrawing = false});

    image.png
    image.png

    image.png
    image.png


    image.png

    不会修改原数组!!


    image.png
    So:
    Use if (arr.length) {}


    image.png

    image.png

    1. class Node {
    2. constructor(data) {
    3. this.data = data;
    4. this.children = [];
    5. }
    6. add(data) {
    7. this.children.push(new Node(data))
    8. }
    9. remove(data) {
    10. this.children = this.children.filter((child) => {
    11. return child.data !== data;
    12. })
    13. }
    14. }
    15. class Tree {
    16. constructor() {
    17. this.root = null;
    18. }
    19. traverseBF(fn) {
    20. const list = [this.root];
    21. while (list.length) {
    22. const node = list.shift();
    23. list.push(...node.children);
    24. fn(node);
    25. }
    26. }
    27. traverseDF(fn) {
    28. const list = [this.root];
    29. while (list.length) {
    30. const node = list.shift();
    31. list.unshift(...node.children);
    32. fn(node);
    33. }
    34. }
    35. }

    image.png

    image.png

    1. class Node {
    2. constructor(data) {
    3. this.data = data;
    4. this.left = null;
    5. this.right = null;
    6. }
    7. // insert 函数不需要 return 什么东西,可以直接回调
    8. insert(data) {
    9. if (this.left && data < this.data) {
    10. this.left.insert(data);
    11. } else if (data < this.data) {
    12. this.left = (new Node(data));
    13. }
    14. if (this.right && data > this.data) {
    15. this.right.insert(data);
    16. } else if (data > this.data) {
    17. this.right = (new Node(data));
    18. }
    19. }
    20. // contain 函数需要 return. 所以回调中要一直 return
    21. contains(data) {
    22. if (data === this.data) {
    23. return this;
    24. } else if (this.left && data < this.data) {
    25. return this.left.contains(data);
    26. } else if (this.right && data > this.data) {
    27. return this.right.contains(data);
    28. } else {
    29. return null;
    30. }
    31. }
    32. }

    image.png