• 前端JavaScript入门到精通,javascript核心进阶ES6语法、API、js高级等基础知识和实战 —— JS进阶(四)完结撒花✿✿ヽ(°▽°)ノ✿


    思维导图

    高阶技巧

    1. 深浅拷贝

    1.1 浅拷贝

    1.2 深拷贝

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <div>div>
    11. <script>
    12. function getTime() {
    13. document.querySelector('div').innerHTML = new Date().toLocaleString()
    14. setTimeout(getTime, 1000)
    15. }
    16. getTime()
    17. script>
    18. body>
    19. html>

    递归函数实现深拷贝

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. const obj = {
    12. uname: 'pink',
    13. age: 18,
    14. hobby: ['乒乓球', '足球'],
    15. family: {
    16. baby: '小pink'
    17. }
    18. }
    19. const o = {}
    20. // 拷贝函数
    21. function deepCopy(newObj, oldObj) {
    22. debugger
    23. for (let k in oldObj) {
    24. // 处理数组的问题 一定先写数组 在写 对象 不能颠倒
    25. if (oldObj[k] instanceof Array) {
    26. newObj[k] = []
    27. // newObj[k] 接收 [] hobby
    28. // oldObj[k] ['乒乓球', '足球']
    29. deepCopy(newObj[k], oldObj[k])
    30. } else if (oldObj[k] instanceof Object) {
    31. newObj[k] = {}
    32. deepCopy(newObj[k], oldObj[k])
    33. }
    34. else {
    35. // k 属性名 uname age oldObj[k] 属性值 18
    36. // newObj[k] === o.uname 给新对象添加属性
    37. newObj[k] = oldObj[k]
    38. }
    39. }
    40. }
    41. deepCopy(o, obj) // 函数调用 两个参数 o 新对象 obj 旧对象
    42. console.log(o)
    43. o.age = 20
    44. o.hobby[0] = '篮球'
    45. o.family.baby = '老pink'
    46. console.log(obj)
    47. console.log([1, 23] instanceof Object)
    48. // 复习
    49. // const obj = {
    50. // uname: 'pink',
    51. // age: 18,
    52. // hobby: ['乒乓球', '足球']
    53. // }
    54. // function deepCopy({ }, oldObj) {
    55. // // k 属性名 oldObj[k] 属性值
    56. // for (let k in oldObj) {
    57. // // 处理数组的问题 k 变量
    58. // newObj[k] = oldObj[k]
    59. // // o.uname = 'pink'
    60. // // newObj.k = 'pink'
    61. // }
    62. // }
    63. script>
    64. body>
    65. html>

    lodash实现深拷贝

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script src="./lodash.min.js">script>
    11. <script>
    12. const obj = {
    13. uname: 'pink',
    14. age: 18,
    15. hobby: ['乒乓球', '足球'],
    16. family: {
    17. baby: '小pink'
    18. }
    19. }
    20. const o = _.cloneDeep(obj)
    21. console.log(o)
    22. o.family.baby = '老pink'
    23. console.log(obj)
    24. script>
    25. body>
    26. html>

    利用JSON实现深拷贝

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. const obj = {
    12. uname: 'pink',
    13. age: 18,
    14. hobby: ['乒乓球', '足球'],
    15. family: {
    16. baby: '小pink'
    17. }
    18. }
    19. // 把对象转换为 JSON 字符串
    20. // console.log(JSON.stringify(obj))
    21. const o = JSON.parse(JSON.stringify(obj))
    22. console.log(o)
    23. o.family.baby = '123'
    24. console.log(obj)
    25. script>
    26. body>
    27. html>

    2.异常处理

    2.1 throw 抛异常

    2.2 try/catch 捕获错误信息

    2.3 debugger

    3.处理this

    3.1 this指向

    普通函数

    箭头函数

    3.2 改变this

    call()

    apply()

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. const obj = {
    12. age: 18
    13. }
    14. function fn(x, y) {
    15. console.log(this) // {age: 18}
    16. console.log(x + y)
    17. }
    18. // 1. 调用函数
    19. // 2. 改变this指向
    20. // fn.apply(this指向谁, 数组参数)
    21. fn.apply(obj, [1, 2])
    22. // 3. 返回值 本身就是在调用函数,所以返回值就是函数的返回值
    23. // 使用场景: 求数组最大值
    24. // const max = Math.max(1, 2, 3)
    25. // console.log(max)
    26. const arr = [100, 44, 77]
    27. const max = Math.max.apply(Math, arr)
    28. const min = Math.min.apply(null, arr)
    29. console.log(max, min)
    30. // 使用场景: 求数组最大值
    31. console.log(Math.max(...arr))
    32. script>
    33. body>
    34. html>

    bind()-重点

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <button>发送短信button>
    11. <script>
    12. const obj = {
    13. age: 18
    14. }
    15. function fn() {
    16. console.log(this)
    17. }
    18. // 1. bind 不会调用函数
    19. // 2. 能改变this指向
    20. // 3. 返回值是个函数, 但是这个函数里面的this是更改过的obj
    21. const fun = fn.bind(obj)
    22. // console.log(fun)
    23. fun()
    24. // 需求,有一个按钮,点击里面就禁用,2秒钟之后开启
    25. document.querySelector('button').addEventListener('click', function () {
    26. // 禁用按钮
    27. this.disabled = true
    28. window.setTimeout(function () {
    29. // 在这个普通函数里面,我们要this由原来的window 改为 btn
    30. this.disabled = false
    31. }.bind(this), 2000) // 这里的this 和 btn 一样
    32. })
    33. script>
    34. body>
    35. html>

    4.性能优化

    4.1 防抖

    lodash提供的防抖处理

    _.debounce(mouseMove, 500)

    手写防抖

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>Documenttitle>
    8. <style>
    9. .box {
    10. width: 500px;
    11. height: 500px;
    12. background-color: #ccc;
    13. color: #fff;
    14. text-align: center;
    15. font-size: 100px;
    16. }
    17. style>
    18. head>
    19. <body>
    20. <div class="box">div>
    21. <script>
    22. const box = document.querySelector('.box')
    23. let i = 1 // 让这个变量++
    24. // 鼠标移动函数
    25. function mouseMove() {
    26. box.innerHTML = ++i
    27. // 如果里面存在大量操作 dom 的情况,可能会卡顿
    28. }
    29. // 防抖函数
    30. function debounce(fn, t) {
    31. let timeId
    32. return function () {
    33. // 如果有定时器就清除
    34. if (timeId) clearTimeout(timeId)
    35. // 开启定时器 200
    36. timeId = setTimeout(function () {
    37. fn()
    38. }, t)
    39. }
    40. }
    41. // box.addEventListener('mousemove', mouseMove)
    42. box.addEventListener('mousemove', debounce(mouseMove, 200))
    43. script>
    44. body>
    45. html>

    4.2 节流

    lodash实现节流

    手动实现节流

    所以使用timer = null 清空定时器

    5.综合案例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <meta name="referrer" content="never" />
    8. <title>综合案例title>
    9. <style>
    10. * {
    11. padding: 0;
    12. margin: 0;
    13. box-sizing: border-box;
    14. }
    15. .container {
    16. width: 1200px;
    17. margin: 0 auto;
    18. }
    19. .video video {
    20. width: 100%;
    21. padding: 20px 0;
    22. }
    23. .elevator {
    24. position: fixed;
    25. top: 280px;
    26. right: 20px;
    27. z-index: 999;
    28. background: #fff;
    29. border: 1px solid #e4e4e4;
    30. width: 60px;
    31. }
    32. .elevator a {
    33. display: block;
    34. padding: 10px;
    35. text-decoration: none;
    36. text-align: center;
    37. color: #999;
    38. }
    39. .elevator a.active {
    40. color: #1286ff;
    41. }
    42. .outline {
    43. padding-bottom: 300px;
    44. }
    45. style>
    46. head>
    47. <body>
    48. <div class="container">
    49. <div class="header">
    50. <a href="http://pip.itcast.cn">
    51. <img src="https://pip.itcast.cn/img/logo_v3.29b9ba72.png" alt="" />
    52. a>
    53. div>
    54. <div class="video">
    55. <video src="https://v.itheima.net/LapADhV6.mp4" controls>video>
    56. div>
    57. <div class="elevator">
    58. <a href="javascript:;" data-ref="video">视频介绍a>
    59. <a href="javascript:;" data-ref="intro">课程简介a>
    60. <a href="javascript:;" data-ref="outline">评论列表a>
    61. div>
    62. div>
    63. <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js">script>
    64. <script>
    65. // 1. 获取元素 要对视频进行操作
    66. const video = document.querySelector('video')
    67. video.ontimeupdate = _.throttle(() => {
    68. // console.log(video.currentTime) 获得当前的视频时间
    69. // 把当前的时间存储到本地存储
    70. localStorage.setItem('currentTime', video.currentTime)
    71. }, 1000)
    72. // 打开页面触发事件,就从本地存储里面取出记录的时间, 赋值给 video.currentTime
    73. video.onloadeddata = () => {
    74. // console.log(111)
    75. video.currentTime = localStorage.getItem('currentTime') || 0
    76. }
    77. script>
    78. body>
    79. html>

  • 相关阅读:
    《鸿蒙生态应用开发白皮书》读后感
    DAC8563数模转换模块的使用介绍
    2019CCPC网络赛 杭电 6705 path(题解+代码)
    npm命令大全
    jetl标签的使用
    大一作业HTML网页作业 HTML校园篮球网页作业(12个页面)
    Java项目:水果生鲜超市商城管理系统(java+SSM+JSP+jQuery+Mysql)
    一份详实的 Scrapy 爬虫教程,从原理到实战
    HTML做一个个人博客页面(纯html代码)
    孩子升年级难适应?猿辅导语文金牌教研来支招
  • 原文地址:https://blog.csdn.net/upgrade_bro/article/details/133789846