Blogger Information
Blog 25
fans 0
comment 0
visits 13500
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS中类的生成及继承,常用的数组和字符串的API
安超
Original
464 people have browsed it

1.JS中的类的生成及继承

S中类用class关键字表示,
类中的几个关键1.构造函数constructor;2.静态数据 :static;3.私有数据:# 4.方法;5.访问器函数 get set;6.继承:extends 继承中super的使用

1.1类的生成

  1. console.log("-------------------------------");
  2. console.log("形成一个User的类\n");
  3. let User = class{
  4. constructor(name ,address,favorite){
  5. this.name = name;
  6. this.address = address;
  7. this.favorite = favorite;
  8. }
  9. static gender = "male";
  10. getUserInfo(){
  11. return `你的姓名是:${this.name}\n你的地址是:${this.address}\n你的爱好是:${this.favorite}\n`;
  12. }
  13. }
  14. let me = new User("jiao","peking","reading");
  15. console.log(me.getUserInfo());

1.2 类的继承 extends

  1. class Child extends User {
  2. constructor(name,address,favorite,tel){ //把父类的全部属性写上
  3. super(name,address,favorite);
  4. this.tel = tel;
  5. }
  6. // 继承父类的方法
  7. getUserInfo(){
  8. return `${super.getUserInfo()}您的电话是:${this.tel}`;
  9. }
  10. }
  11. let child_1 = new Child("wang","hefei","sport","1388");
  12. console.log(child_1.getUserInfo());

1.3 类的访问器属性

  1. class Descendent extends Child{
  2. constructor(name,address,favorite,tel,age){
  3. super(Child);
  4. this.age= age;
  5. }
  6. #gender = "male";
  7. get gender() {
  8. return this.#gender;
  9. }
  10. set gender(a) {
  11. this.#gender = a;
  12. }
  13. }
  14. let des_1 = new Descendent("li","sd","reading","323",32);
  15. console.log(des_1.gender);
  16. console.log("变换gender的值");
  17. des_1.gender = "female";
  18. console.log(des_1.gender);

2.字符串中常用的API

  1. let str = "hello the world ";
  2. let str_2 ="again";
  3. // 2.1indexOf查找字符串中某个字符第一次出现的位置;
  4. console.log(str.indexOf("l"));
  5. // 2.2 concat合并两个字符串
  6. console.log(str.concat(str_2));
  7. // 2.3 slice() 获得指定位置之后的片段字符
  8. console.log(str.slice(3))
  9. // 2.4 replace( ),取代字符串中的某些字符
  10. console.log(str.replace("the","my"));
  11. // 2.5 匹配字符中的某些字符
  12. console.log(str.match(/the/));
  13. console.log('--------------------------');

3.数组常用的API

  1. console.log("数组常用的API");
  2. let arr_1 = [1,2,30,23];
  3. let arr_2 = [4,5];
  4. //3.1 求和 reduce
  5. console.log(arr_1.reduce((acc,cur)=> acc + cur));
  6. // 3.2. 遍历 map() 并返回数组
  7. let arr_map = arr_1.map(cur => cur +=2);
  8. console.log(arr_map);
  9. // 3.3 concat()合并数组
  10. console.log(arr_1.concat(arr_2));
  11. // 3.4 取得数组中的片段
  12. console.log(arr_1.slice(2));
  13. // 3.5 数组扩项与减项 pop push
  14. console.log(arr_1.pop(3));
  15. console.log(arr_1);
  16. console.log(arr_1.push(45));
  17. console.log(arr_1);
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!