博客列表 >class类与extends,super等的用法与字符串,数组常用API

class类与extends,super等的用法与字符串,数组常用API

牙森江
牙森江原创
2022年11月08日 13:10:24326浏览

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

1.1 class类

=>1.构造函数并声明->属性->2.方法->3.静态成员

  1. let User = class {
  2. constructor(uname, email) {
  3. this.uname = uname
  4. this.email = email
  5. }
  6. say() {
  7. return `${this.uname}: ( ${this.email} )`
  8. }
  9. static nation = '中国'
  10. }
  11. const user = new User('Mr.Y', 'mmmyyy@qq.com')
  12. console.log(user.say())
  13. console.log(User.nation)
  14. 运行结果:
  15. Mr.Y: ( mmmyyy@qq.com )
  16. 中国

1.2 extends,super的用法

  1. class Child extends User {
  2. constructor(uname, email, gender) {
  3. super(uname, email)
  4. this.gender = gender
  5. }
  6. say() {
  7. return `${super.say()}, (${this.gender})`
  8. }
  9. }
  10. const child = new Child('黄氏', 'hs@qq.com', '男')
  11. console.log(child.say())
  12. 运行结果:
  13. 黄氏: ( hs@qq.com ), (男)

2.字符串,数组常用API

2.1.length 属性

  1. let xName = 'My name is Yasinjan'
  2. console.log('length = ', xName.length)
  3. 运行结果:
  4. length = 19

2.2 charAt(), 索引 -> 成员

  1. let xName = 'My name is Yasinjan'
  2. console.log(xName[4])
  3. 运行结果:
  4. a

2.3 indexOf(): 成员 -> 索引

  1. let xName = 'My name is Yasinjan'
  2. console.log(xName.indexOf('Y'))
  3. 运行结果:
  4. 11

2.4 replace()替换

  1. let xName = 'My name is Yasinjan'
  2. console.log(xName.replace('Yasinjan', 'GongLi'))
  3. 运行结果:
  4. My name is GongLi

2.5 split: 字符串 -> 数组

  1. let xName = 'My name is Yasinjan'
  2. console.log(xName.split('', 5))
  3. 运行结果:
  4. [ 'M', 'y', ' ', 'n', 'a' ]
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议