• 前端JavaScript入门到精通,javascript核心进阶ES6语法、API、js高级等基础知识和实战 —— JS进阶(三)


    思维导图

    1.编程思想

    1.1 面向过程编程

    1.2 面向对象编程 (oop)

    2. 构造函数

    3. 原型

    3.1 原型

    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. // 自己定义 数组扩展方法 求和 和 最大值
    12. // 1. 我们定义的这个方法,任何一个数组实例对象都可以使用
    13. // 2. 自定义的方法写到 数组.prototype 身上
    14. // 1. 最大值
    15. const arr = [1, 2, 3]
    16. Array.prototype.max = function () {
    17. // 展开运算符
    18. return Math.max(...this)
    19. // 原型函数里面的this 指向谁? 实例对象 arr
    20. }
    21. // 2. 最小值
    22. Array.prototype.min = function () {
    23. // 展开运算符
    24. return Math.min(...this)
    25. // 原型函数里面的this 指向谁? 实例对象 arr
    26. }
    27. console.log(arr.max())
    28. console.log([2, 5, 9].max())
    29. console.log(arr.min())
    30. // const arr = new Array(1, 2)
    31. // console.log(arr)
    32. // 3. 求和 方法
    33. Array.prototype.sum = function () {
    34. return this.reduce((prev, item) => prev + item, 0)
    35. }
    36. console.log([1, 2, 3].sum())
    37. console.log([11, 21, 31].sum())
    38. script>
    39. body>
    40. html>

    3.2 constructor 属性

    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. // constructor 单词 构造函数
    12. // Star.prototype.sing = function () {
    13. // console.log('唱歌')
    14. // }
    15. // Star.prototype.dance = function () {
    16. // console.log('跳舞')
    17. // }
    18. function Star() {
    19. }
    20. // console.log(Star.prototype)
    21. Star.prototype = {
    22. // 从新指回创造这个原型对象的 构造函数
    23. constructor: Star,
    24. sing: function () {
    25. console.log('唱歌')
    26. },
    27. dance: function () {
    28. console.log('跳舞')
    29. },
    30. }
    31. console.log(Star.prototype)
    32. // console.log(Star.prototype.constructor)
    33. // const ldh = new Star()
    34. // console.log(Star.prototype.constructor === Star)
    35. script>
    36. body>
    37. html>

    3.3 对象原型

    注意区别:对象原型 与 原型(对象)

    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. function Star() {
    12. }
    13. const ldh = new Star()
    14. // 对象原型__proto__ 指向 改构造函数的原型对象
    15. console.log(ldh.__proto__)
    16. // console.log(ldh.__proto__ === Star.prototype)
    17. // 对象原型里面有constructor 指向 构造函数 Star
    18. console.log(ldh.__proto__.constructor === Star)
    19. script>
    20. body>
    21. html>

    3.4 原型继承

    3.5 原型链

    实例对象 instanceof 构造函数

    4.综合案例

    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>面向对象封装消息提示title>
    8. <style>
    9. .modal {
    10. width: 300px;
    11. min-height: 100px;
    12. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    13. border-radius: 4px;
    14. position: fixed;
    15. z-index: 999;
    16. left: 50%;
    17. top: 50%;
    18. transform: translate3d(-50%, -50%, 0);
    19. background-color: #fff;
    20. }
    21. .modal .header {
    22. line-height: 40px;
    23. padding: 0 10px;
    24. position: relative;
    25. font-size: 20px;
    26. }
    27. .modal .header i {
    28. font-style: normal;
    29. color: #999;
    30. position: absolute;
    31. right: 15px;
    32. top: -2px;
    33. cursor: pointer;
    34. }
    35. .modal .body {
    36. text-align: center;
    37. padding: 10px;
    38. }
    39. .modal .footer {
    40. display: flex;
    41. justify-content: flex-end;
    42. padding: 10px;
    43. }
    44. .modal .footer a {
    45. padding: 3px 8px;
    46. background: #ccc;
    47. text-decoration: none;
    48. color: #fff;
    49. border-radius: 2px;
    50. margin-right: 10px;
    51. font-size: 14px;
    52. }
    53. .modal .footer a.submit {
    54. background-color: #369;
    55. }
    56. style>
    57. head>
    58. <body>
    59. <button id="delete">删除button>
    60. <button id="login">登录button>
    61. <script>
    62. // 1. 模态框的构造函数
    63. function Modal(title = '', message = '') {
    64. // 公共的属性部分
    65. this.title = title
    66. this.message = message
    67. // 因为盒子是公共的
    68. // 1. 创建 一定不要忘了加 this
    69. this.modalBox = document.createElement('div')
    70. // 2. 添加类名
    71. this.modalBox.className = 'modal'
    72. // 3. 填充内容 更换数据
    73. this.modalBox.innerHTML = `
    74. ${this.title} x
    75. ${this.message}
    76. `
    77. // console.log(this.modalBox)
    78. }
    79. // 2. 打开方法 挂载 到 模态框的构造函数原型身上
    80. Modal.prototype.open = function () {
    81. if (!document.querySelector('.modal')) {
    82. // 把刚才创建的盒子 modalBox 渲染到 页面中 父元素.appendChild(子元素)
    83. document.body.appendChild(this.modalBox)
    84. // 获取 x 调用关闭方法
    85. this.modalBox.querySelector('i').addEventListener('click', () => {
    86. // 箭头函数没有this 上一级作用域的this
    87. // 这个this 指向 m
    88. this.close()
    89. })
    90. }
    91. }
    92. // 3. 关闭方法 挂载 到 模态框的构造函数原型身上
    93. Modal.prototype.close = function () {
    94. document.body.removeChild(this.modalBox)
    95. }
    96. // 4. 按钮点击
    97. document.querySelector('#delete').addEventListener('click', () => {
    98. const m = new Modal('温馨提示', '您没有权限删除')
    99. // 调用 打开方法
    100. m.open()
    101. })
    102. // 5. 按钮点击
    103. document.querySelector('#login').addEventListener('click', () => {
    104. const m = new Modal('友情提示', '您还么有注册账号')
    105. // 调用 打开方法
    106. m.open()
    107. })
    108. script>
    109. body>
    110. html>

    多次点击生成多个盒子bug解决方法②:

  • 相关阅读:
    Day22
    MySQL 保姆级教程(六):用通配符进行过滤
    Mac中隐私安全性设置-打开任何来源
    常用的C语法:指针和数组名的区别
    Spring复习——day16_SpringMVC_异常处理器
    MD5 加密安全吗?
    受众分析与卸载分析全面升级,HMS Core分析服务6.6.0版本上新
    23种设计模式之代理模式(动态代理)
    SpringBoot+ MDC实现全链路调用日志跟踪
    UNIX网络编程卷一 学习笔记 第二章 传输层:TCP、UDP和SCTP
  • 原文地址:https://blog.csdn.net/upgrade_bro/article/details/133772424