Blogger Information
Blog 25
fans 0
comment 0
visits 10571
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
class类与extends,super等的用法与字符串,数组常用API
PHui
Original
324 people have browsed it

1.class类与extends,super等的用法

  1. let User = class {
  2. // 构造函数:声明属性
  3. constructor(uname, email) {
  4. // 1.属性
  5. this.uname = uname;
  6. this.email = email;
  7. }
  8. // 2.方法
  9. say() {
  10. return `${this.uname}:(${this.email})`;
  11. }
  12. // 3.静态成员
  13. static nation = "CN";
  14. };
  15. const user = new User("libai", "113@qq.com");
  16. console.log(user.say());
  17. console.log(User.nation);
  18. // extends继承
  19. class Child extends User {
  20. constructor(uname, email, sex) {
  21. // super 调用父类成员
  22. super(uname, email);
  23. this.sex = sex;
  24. }
  25. say() {
  26. return `${super.say()},(${this.sex})`;
  27. }
  28. }
  29. const child = new Child("sushi", "567@qq.com", "man");
  30. console.log(child.say());

2.字符串常用API

  1. let str = "I Love My Country";
  2. // 1. length 属性
  3. console.log("length = ", str.length);
  4. // 2. charAt(), 索引 -> 成员
  5. console.log(str[5]);
  6. // 3. indexOf(): 成员 -> 索引
  7. console.log(str.indexOf("y"));
  8. // 4. replace()替换
  9. console.log(str);
  10. console.log(str.replace("Country", "Mother"));
  11. // 5. substring() ,必须去掉(忽略)结束索引
  12. console.log(str.substring(2, 6));
  13. // 6. split: 字符串 -> 数组
  14. console.log(str.split(""));
  15. // 7. 大小写
  16. console.log(str.toLowerCase());
  17. console.log(str.toUpperCase());

3.数组常用API

  1. //...rest 压缩与展开
  2. arr = [1, 2, 3];
  3. let arr1 = [...arr];
  4. console.log(arr1);
  5. arr = [...arr, 4, 5, 6];
  6. console.log(arr);
  7. // Array.of 打包
  8. let items = [1, 2, 3, 4, 5, 6];
  9. console.log(Array.of(...items));
  10. // Array.from() 类数组 -> 真正的数组
  11. const likeArr = {
  12. 0: "red",
  13. 1: "blue",
  14. 2: "green",
  15. length: 3,
  16. };
  17. const data = Array.from(likeArr);
  18. console.log(Array.isArray(data) ? "Array" : "no Array");
  19. for (let item of data) {
  20. console.log(item);
  21. }
  22. let arr2 = [];
  23. // push 尾部插入
  24. console.log(arr2.push(10));
  25. console.log(arr2);
  26. console.log(arr2.push(20, 30));
  27. console.log(arr2);
  28. // pop 尾部弹出
  29. console.log(arr2.pop());
  30. console.log(arr2.pop());
  31. console.log(arr2);
  32. // unshift 头部添加
  33. console.log(arr2.unshift(10));
  34. console.log(arr2);
  35. console.log(arr2.unshift(20, 30, 40));
  36. console.log(arr2);
  37. // shift 头部弹出
  38. console.log(arr2.shift());
  39. console.log(arr2.shift());
  40. console.log(arr2);
  41. // delete
  42. delete arr2[0];
  43. console.log(arr2);

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