Blogger Information
Blog 16
fans 0
comment 0
visits 5690
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性/字符串和数组方法
Sinan学习博客
Original
464 people have browsed it

访问器属性/字符串和数组方法

1. 实例演示访问器属性

  1. //对象属性传统访问方式
  2. let obj = {
  3. item: {
  4. price: 5000,
  5. },
  6. };
  7. console.log(obj.item.price); //传统访问
  8. //更新
  9. obj.item.price = 7000;
  10. console.log(obj.item.price); //传统访问
  11. //访问器属性方式
  12. obj = {
  13. item: {
  14. price: 5000,
  15. },
  16. get price() {
  17. return this.item.price;
  18. },
  19. set price(price) {
  20. this.item.price = price;
  21. },
  22. };
  23. console.log(obj.price);
  24. console.log((obj.price = 9000));

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

字符串方法

  1. str.length
  2. str.search()
  3. str.replace()
  4. str.slice()
  5. str.substr()
  6. str.split()
  7. str.toLowerCase()
  8. str.toUpperCase()
  1. let str = "php中文网";
  2. // 1. length: 获取字符串的长度
  3. console.log(str.length); //6
  4. // 2. search(): 返回字符的索引
  5. console.log(str.search("中")); //3
  6. // 3. replace(): 字符替换
  7. console.log(str.replace("中文网", ".cn")); //php.cn
  8. // 4. slice(): 查询子串
  9. //第一个参数是起始索引,第二个参数是结束索引,忽略最后一个索引的值
  10. console.log(str.slice(0, 3)); //php
  11. // 5. substr(): 查询子串
  12. //第一个参数是起始索引,第二个参数是索引个数
  13. console.log(str.substr(0, 3));
  14. // 6. split(): 把字符串转为数组
  15. console.log(str.split(""));
  16. // 7. toLowerCase(): 把字母转为小写
  17. console.log("Hello World".toLowerCase());
  18. // 8. toUpperCase(): 把字母转为大写
  19. console.log("abc".toUpperCase());

数组的方法

  1. //push():添加至尾部,并返回当前数组中元素个数
  2. let arr = []; //创建空数组arr
  3. console.log(arr.push("a")); //添加字符a,输出1
  4. console.log(arr.push("b", "c")); //在a后面又添加了2个字符b,c,输出3
  5. console.log(arr); //输出数组arr观察:[ 'a', 'b', 'c' ]
  6. //pop():从尾部删除,并返回被删除的元素
  7. console.log(arr.pop()); //c
  8. console.log(arr); //["a", "b"];
  9. console.log(arr.pop()); //b
  10. console.log(arr); //["a"];
  11. //unshift():添加至头部,并返回当前数组中元素个数
  12. let arr1 = [];
  13. console.log(arr1.unshift("A")); //添加字符A,输出1
  14. console.log(arr1); //[ 'A' ]
  15. console.log(arr1.unshift("C", "B")); //添加字符C,B,输出3
  16. console.log(arr1); //[ 'C', 'B', 'A' ]
  17. //shift():从头部删除,并返回被删除的元素
  18. console.log(arr1.shift()); //C
  19. console.log(arr1); //[ 'B', 'A' ]
  20. console.log(arr1.shift()); //B
  21. console.log(arr1); //[ 'A' ]
  22. console.log(arr1.shift()); //A
  23. console.log(arr1); //[]
  24. //删除任意位置的元素
  25. let arr2 = [10, 20, 30, 40, 50];
  26. //使用delete删除指定的数组索引的元素,返回一个布尔值,true表示删除成功,false表示删除失败。
  27. console.log(delete arr2[2]); //true
  28. console.log(arr2); //[ 10, 20, <1 empty item>, 40, 50 ],delete删除了元素的值,但是他的位置还在,是空元素。

3. 实战之循环队列

使用while循环

  1. let arr2 = [1, 2, 3, 4];
  2. let num2,
  3. i = 0;
  4. while (i <= 5) {
  5. console.log(arr2);
  6. num2 = arr2.shift();
  7. arr2.push(num2);
  8. i++;
  9. }

使用for循环

  1. let arr1 = [1, 2, 3, 4];
  2. let num1 = 0;
  3. console.log(arr1);
  4. for (let i = 0; i < 5; i++) {
  5. num1 = arr1.shift();
  6. arr1.push(num1);
  7. console.log(arr1);
  8. }

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