Blogger Information
Blog 19
fans 0
comment 1
visits 7422
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
class类和常用API
移动用户-4050479
Original
337 people have browsed it
  1. // * class 类 类声明
  2. class User {
  3. // * 构造函数 :声明属性
  4. constructor ( uname , emj ){
  5. this.uname = uname
  6. this.emj = emj
  7. }
  8. so () {
  9. return `${this.uname}!= ${this.emj}`
  10. }
  11. static MOVE = 'CCCCC'
  12. }
  13. // * 实例化类
  14. const user = new User ('3123' , '2334')
  15. // * 访问类
  16. console.log(user.so());
  17. // * 继承
  18. class Pv extends User {
  19. constructor(uname , emj , cv) {
  20. // * super 调用父类成员
  21. super( uname , emj )
  22. this.cv = cv
  23. }
  24. so () { // * ${super.so()} 调用父类的方法
  25. return `${super.so()} - ${this.cv}`
  26. }
  27. }
  28. const user1 = new Pv ('3123' , '2334' , 'xcx')
  29. console.log(user1.so());
  30. // ! 类中使用 "访问器属性"
  31. const Svg = class {
  32. // * 声明私有属性 在属性名前面加上 #
  33. #xc = 12
  34. get age () {
  35. return this.#xc
  36. }
  37. // * 设置访问器 set
  38. set age(age) {
  39. age >= 18 ? (age <= 100 ? this.#xc = age :console.log('非法')) :console.log('非法');
  40. }
  41. }
  42. // * 实例化
  43. const svv = new Svg ()
  44. svv.age = 17
  45. console.log(svv.age);
  46. //* ********************************************************************** */
  47. let str = '123456'
  48. console.log('--------------------');
  49. // * 获取字符串长度
  50. console.log(str.length);
  51. //* charAt() 根据索引拿到成员 */
  52. console.log(str.charAt(0) );
  53. console.log(str[0]);
  54. // * indexof 由成员获取索引
  55. console.log(str.indexOf('4'));
  56. console.log(str.includes('5')); // * 查询数据是否在字符串中
  57. console.log(str.concat('456' , '789')); // * 字符串拼接
  58. console.log(str.replace('34','aaa')); // * 字符替换
  59. console.log(str.substring(0,3)); // * 必须忽略结束索引 ,即最后一个数据
  60. console.log(str.split()); // * 字符串转为数组
  61. console.log(str.split('',4)); // * 字符串转为数组 第二个参数指定获取几位
  62. console.log('ANA'.toLocaleLowerCase()); // * 转小写
  63. console.log(str.link('baidu.com')); // * 生成链接
  64. // * ******************************************** * /
  65. let arr = []
  66. console.log(arr.push(10,23,789)); //* 尾部差入数据
  67. console.log(arr);
  68. // * 删除
  69. console.log(arr.pop());
  70. console.log(arr.pop());
  71. console.log(arr.pop());
  72. console.log(arr.pop());
  73. console.log('---------------------');
  74. // * 头部添加
  75. console.log(arr.unshift(10,654,56,565,78));
  76. console.log(arr);
  77. console.log(arr.shift());
  78. console.log('--------------');
  79. delete arr[2]
  80. console.log(arr);
  81. // *********************************************** //! WARNING
  82. // * 迭代 for each 没有返回值
  83. arr.forEach (function(item){
  84. console.log(item);
  85. })
  86. console.log('=---------------------');
  87. // * map 有返回值
  88. arr.map (function(item){
  89. return console.log(item);
  90. })
  91. console.log('=---------------------');
  92. // *
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