Blogger Information
Blog 17
fans 0
comment 0
visits 6102
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性、数组和字符串常用方法、循环队列(试水轮播图)
生活需要加油
Original
349 people have browsed it

访问器属性、数组和字符串常用方法、循环队列

1. 实例演示访问器属性

  1. let prj = {
  2. prjn: { prjname: "环球金融中心" },
  3. getPrj() {
  4. return this.prjn.prjname
  5. },
  6. setPrj(prjname) {
  7. this.prjn.prjname = prjname
  8. },
  9. }
  10. console.log(prj.getPrj())
  11. prj.setPrj("上海中心大厦")
  12. console.log(prj.getPrj())

执行结果如下:

2. 实例演示课堂上列出的所有字符串和数组方法

2.1 字符串方法

  1. //! 1.length
  2. let str = "上海中心大厦"
  3. console.log(str.length)
  4. console.log("--------------------")
  5. //! 2.search
  6. console.log(str.search("心"))
  7. console.log("--------------------")
  8. //! 3.replace
  9. console.log(str.replace("中心", "广播"))
  10. console.log(str)
  11. console.log("--------------------")
  12. //! 4.slice
  13. console.log(str.slice(2, 4))
  14. console.log("--------------------")
  15. //! 5.substr
  16. console.log(str.substr(2, 4))
  17. console.log("--------------------")
  18. //! 6.split/join(数组方法)
  19. console.log(str.split(""))
  20. str = "上,海,中,心,大,厦"
  21. console.log(str.split(","))
  22. console.log(str.split(",").join(""))
  23. console.log("--------------------")
  24. // 7. toLowerCase()/toUpperCase()
  25. str = "ShangHai Central Building"
  26. console.log(str.toUpperCase())
  27. console.log(str.toLowerCase())
  28. console.log("--------------------")

执行结果如下:

2.2 数组方法

2.2.1 尾部:添加与删除, push->返回当前数组元素的数量,pop->返回移出来的元素

  1. //! 1. 尾部:添加与删除, push->返回当前数组元素的数量,pop->返回移出来的元素
  2. let arr = []
  3. console.log(arr.push(10))
  4. console.log(arr)
  5. console.log(arr.push(20, 30))
  6. console.log(arr)
  7. console.log("----------------------------")
  8. console.log(arr.pop())
  9. console.log(arr)
  10. console.log("============================")
  11. // 2. 头部:添加与删除, unshift,shift
  12. console.log(arr.unshift(30))
  13. console.log(arr)
  14. console.log("----------------------------")
  15. console.log(arr.shift(30))
  16. console.log(arr)
  17. console.log(arr.unshift(30, 40, 50))
  18. console.log(arr)
  19. console.log("============================")
  20. // 删除任意位置
  21. console.log(delete arr[1])
  22. console.log(arr)
  23. console.log(arr.length)
  24. let arr1 = arr.filter(item => item)
  25. console.log(arr1)
  26. console.log("============================")
  27. //! 3`arr.keys()/arr.values()/arr.entries()`
  28. console.log(arr1)
  29. for (let item of arr1.keys()) {
  30. console.log(item)
  31. }
  32. for (let item of arr1.values()) {
  33. console.log(item)
  34. }
  35. for (let item of arr1.entries()) {
  36. console.log(item)
  37. }
  38. console.log("============================")
  39. //! 4`arr.slice()` 返回起始位置不含结束位置的元素
  40. console.log(arr1.slice(1, 3))
  41. console.log(arr1)
  42. console.log("============================")
  43. console.log(arr1.push(60, 70, 80, 90))
  44. console.log(arr1)
  45. //! 5`arr.splice()`
  46. // 5.1 删除
  47. console.log(arr1.splice(1, 3))
  48. //返回被删除元素组成的数组
  49. console.log(arr1)
  50. console.log("----------------------------")
  51. // 5.2 增加
  52. console.log(arr1.splice(1, 0, 10, 20, 30, 40))
  53. //返回空数组,因为没有删除内容
  54. console.log(arr1)
  55. console.log("----------------------------")
  56. // 5.3更新
  57. console.log(arr1.splice(1, 4, "aa", "cc", "cc", "dc"))
  58. console.log(arr1)
  59. console.log("============================")
  60. //! 6`arr.sort()`
  61. console.log(arr1.splice(1, 4, 10, 20, 40, 50))
  62. console.log(arr1)
  63. console.log(
  64. arr1.sort(function (a, b) {
  65. return a - b
  66. })
  67. )
  68. //箭头函数表示
  69. console.log(arr1.sort((a, b) => b - a))
  70. console.log(arr1.sort((a, b) => a - b))
  71. console.log("============================")
  72. //! 7`arr.forEach()/arr.map()`
  73. arr1.forEach(item => console.log(item * 2))
  74. // forEach没有返回值,map有返回值
  75. res = arr1.map(item => item * 2)
  76. console.log(res)
  77. console.log("============================")
  78. //! 8`arr.some()/arr.every()`
  79. // some: 类似于||
  80. // every: 类似于 &&
  81. console.log(arr1)
  82. console.log(arr1.every(item => item > 50))
  83. console.log(arr1.every(item => item > 0))
  84. console.log(arr1.some(item => item > 50))
  85. console.log("============================")
  86. //! 9`arr.filter()/find()/findLast()/findIndex()`
  87. // 返回满足条件的元素,组成的新数组
  88. res = arr1.filter(item => item > 50)
  89. console.log(res)
  90. // 获取满足条件的第几个值
  91. res = arr1.filter(item => item > 50)[1]
  92. console.log(res)
  93. res = arr1.filter(item => item > 50)[0]
  94. console.log(res)
  95. // arr.find()找满足条件的第一个值,等同于arr.filter(item => item > 50)[0]
  96. res = arr1.find(item => item > 50)
  97. console.log(res)
  98. // arr.findlast()返回满足条件的最后一个值
  99. res = arr1.findLast(item => item > 50)
  100. console.log(res)
  101. // 返回满足条件第一个值的索引
  102. console.log("------------------------")
  103. res = arr1.findIndex(item => item > 50)
  104. console.log(res)
  105. console.log("------------------------")
  106. // 返回满足条件最后一个值的索引,需分两步
  107. let val = arr1.findLast(item => item > 50)
  108. console.log(val)
  109. res = arr1.lastIndexOf(val)
  110. console.log(res)
  111. console.log("============================")
  112. //! 10`arr.reduce()`
  113. // reduce(callback, init)
  114. // 语法: reduce(function (前一个值,当前值){
  115. //...
  116. // 最终的累加结果,由前一个值返回
  117. // })
  118. res = arr1.reduce((prev, curr) => prev + curr)
  119. console.log(res)

运行结果如下(有点长,分段截图):


3. 实战之循环队列(尝试图片轮播)

  1. <!DOCTYPE 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. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. li {
  15. list-style: none;
  16. }
  17. ul {
  18. margin: 0 auto;
  19. height: 300px;
  20. /* width: 1000px; */
  21. display: flex;
  22. flex-flow: row nowrap;
  23. place-content: space-between;
  24. flex: auto;
  25. place-items: center;
  26. }
  27. ul > li:first-child,
  28. ul > li:last-child {
  29. width: 50px;
  30. }
  31. ul > li:first-child,
  32. ul > li:last-child {
  33. width: 50px;
  34. }
  35. ul > li:nth-child(2n) {
  36. width: 100px;
  37. }
  38. ul > li:nth-child(3) {
  39. width: 500px;
  40. }
  41. img {
  42. width: 100%;
  43. height: 100%;
  44. border-radius: 30px;
  45. box-shadow: 10px;
  46. transition: 0.5s;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <ul id="lunb"></ul>
  52. </body>
  53. <script>
  54. let arr = [
  55. ["pic0.png", "www.baidu.com", "百度网"],
  56. ["pic1.png", "www.php.cn", "php中文网"],
  57. ["pic2.png", "www.163.com", "网易"],
  58. ["pic3.png", "www.taobao.com", "淘宝"],
  59. ["pic4.png", "www.jd.com", "京东"],
  60. ]
  61. let index = 0
  62. function lunbt(arr, index) {
  63. arr.push(arr.shift())
  64. index++
  65. if (index >= arr.length) {
  66. index = 0
  67. }
  68. let res1 = arr.reduce((prev, curr) =>`${prev}<li><a href="${curr[1]}"><image src="images/${curr[0]}" alt="${curr[2]}}"></image></a></li>`,'')
  69. document.getElementById("lunb").innerHTML = res1
  70. }
  71. setInterval("lunbt(arr, index)", 2000)
  72. </script>
  73. </html>

运行结果如下(效果有点矬):

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:总体来看还不错,继续加油
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!