Blogger Information
Blog 38
fans 0
comment 0
visits 22646
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串与数组方法以及class的用法
一个好人
Original
452 people have browsed it

队列与循环队列

  1. // 队列
  2. arr = ['a','b','c','d']
  3. function que1(arr, str){
  4. arr.push(str)
  5. arr.shift()
  6. }
  7. console.log(arr);
  8. que1(arr, 'e')
  9. console.log(arr);
  10. que1(arr, 'f')
  11. console.log(arr);
  12. // 循环队列
  13. arr = ['a','b','c','d']
  14. function que2(arr){
  15. let a = arr.shift()
  16. arr.push(a)
  17. }
  18. console.log(arr);
  19. que2(arr)
  20. console.log(arr);
  21. que2(arr)
  22. console.log(arr);
  23. for(let key of arr.entries()){
  24. console.log(key);
  25. }
  1. 列队
  2. (4) ['a', 'b', 'c', 'd']
  3. (4) ['b', 'c', 'd', 'e']
  4. (4) ['c', 'd', 'e', 'f']
  5. 循环列队
  6. (4) ['a', 'b', 'c', 'd']
  7. (4) ['b', 'c', 'd', 'a']
  8. (4) ['c', 'd', 'a', 'b']

演示字符串与数组方法(至少3个)

  1. str = 'Dmegc东磁'
  2. console.log(str[3])
  3. console.log(str.length)
  4. console.log(str.search('e'))
  5. console.log(str.replace('e','u'))
  6. console.log(str.slice(0,5)) // 拿不到结束索引
  7. console.log(str.substr(5,2))
  8. console.log(str.split('')) // 切割成数组
  9. console.log(str.toLowerCase())
  10. console.log(str.toUpperCase())
  1. g
  2. 7
  3. 2
  4. Dmugc东磁
  5. Dmegc
  6. 东磁
  7. Array(7)
  8. dmegc东磁
  9. DMEGC东磁
  1. let arr = []
  2. arr.push('a','b','c')
  3. console.log(arr)
  4. arr.pop()
  5. console.log(arr)
  6. arr.unshift('A')
  7. console.log(arr)
  8. arr.shift()
  9. console.log(arr);
  10. for(let key of arr.entries()){
  11. console.log(key);
  12. }
  13. // arr.keys()/arr.values()/arr.entries()用法相同,得到迭代对象
  14. console.log(arr.slice(2, 4));
  15. console.log(arr.includes('a'));
  16. console.log(arr.splice(3, 4));
  17. // arr.splice() 前值为起始位置,后值为删除元素个数,删除对应的数组,并返回;如果有三值,则为插入的元素,如果插入元素为数组,想展开可加...
  18. // 排序,a-b升序,b-a降序
  19. arr = [3,5,1,9]
  20. arr.sort(function(a,b){
  21. return a - b
  22. })
  23. console.log(arr);
  24. arr.sort((a,b)=> b - a) // 箭头函数简化
  25. console.log(arr);
  26. // arr.forEach()
  27. // arr.map( )
  28. // arr.some()是含有即为true,every()是全部满足为true
  29. console.log(arr.every(item=>item > 5)); // 参数支持item值,index索引,arr数组
  30. // arr.filter() 返回满足条件的新数组
  31. console.log(arr.filter(item=>item > 5));
  32. // find() 获取满足条件的第一个元素
  33. //findLast() 获取满足条件的最后一个元素
  34. //findIndex()
  35. // arr.reduce()累加器
  36. console.log(arr.reduce((acc, cur)=>acc+cur));
  1. (3) ['a', 'b', 'c']
  2. (2) ['a', 'b']
  3. (3) ['A', 'a', 'b']
  4. (2) ['a', 'b']
  5. (2) [0, 'c']
  6. (2) [1, 'd']
  7. (2) [2, 'a']
  8. (2) [3, 'b']
  9. (2) ['a', 'b']
  10. true
  11. ['b']
  12. (4) [1, 3, 5, 9]
  13. (4) [9, 5, 3, 1]
  14. false
  15. [9]
  16. 18

演示class的用法(属性,方法,访问器属性,继承)

  1. function Code(nd, fe, b){
  2. this.nd = nd
  3. this.fe = fe
  4. this.b = b
  5. }
  6. Code.prototype.show = function(){
  7. return `${this.nd}:[${this.fe}]` //以自定义格式输出
  8. }
  9. let code = new Code(28, 68, 1)
  10. console.log(code);
  11. console.log(code.show());
  12. // ES6中的类,是构造方法
  13. /**
  14. * 内部成员间没有,
  15. * 必须用简写
  16. */
  17. class Grade{
  18. constructor(br, hcj){
  19. this.br = br
  20. this.hcj = hcj
  21. }
  22. show(){
  23. return `${this.br}:${this.hcj}`
  24. }
  25. // 静态成员
  26. static company = 'dmegc'
  27. }
  28. // 访问
  29. const N48H = new Grade(13.6, 17)
  30. console.log(N48H);
  31. console.log(N48H.show());
  32. console.log(Grade.company);
  33. // 静态成员
  34. let f = function(){}
  35. f.a = 'hello'
  36. console.log(f.a);
  37. class Formula extends Grade{
  38. constructor(br, hcj, d){
  39. super(br,hcj)
  40. this.d = d
  41. }
  42. /**
  43. * 可以引入访问器,同名时访问器属性大于原始属性
  44. */
  45. show(){
  46. return `${super.show()}, ${this.d}`
  47. }
  48. }
  49. const N48H01 = new Formula(13.6, 17, 7.5)
  50. console.log(N48H01.show());
  51. N48H01.d = 7.55
  52. console.log(N48H01.show());
  1. Code {nd: 28, fe: 68, b: 1}
  2. 28:[68]
  3. Grade {br: 13.6, hcj: 17}
  4. 13.6:17
  5. dmegc
  6. hello
  7. 13.6:17, 7.5
  8. 13.6:17, 7.55

总结:

进入js需要练习的东西多了,class要好好联系一下,用好了处理数据很有用的,目前最头疼的是众多数据如何设计数据表。

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