1.class类与extends,super等的用法
1.1 class类
=>1.构造函数并声明->属性->2.方法->3.静态成员
let User = class {
constructor(uname, email) {
this.uname = uname
this.email = email
}
say() {
return `${this.uname}: ( ${this.email} )`
}
static nation = '中国'
}
const user = new User('Mr.Y', 'mmmyyy@qq.com')
console.log(user.say())
console.log(User.nation)
运行结果:
Mr.Y: ( mmmyyy@qq.com )
中国
1.2 extends,super的用法
class Child extends User {
constructor(uname, email, gender) {
super(uname, email)
this.gender = gender
}
say() {
return `${super.say()}, (${this.gender})`
}
}
const child = new Child('黄氏', 'hs@qq.com', '男')
console.log(child.say())
运行结果:
黄氏: ( hs@qq.com ), (男)
2.字符串,数组常用API
2.1.length 属性
let xName = 'My name is Yasinjan'
console.log('length = ', xName.length)
运行结果:
length = 19
2.2 charAt(), 索引 -> 成员
let xName = 'My name is Yasinjan'
console.log(xName[4])
运行结果:
a
2.3 indexOf(): 成员 -> 索引
let xName = 'My name is Yasinjan'
console.log(xName.indexOf('Y'))
运行结果:
11
2.4 replace()替换
let xName = 'My name is Yasinjan'
console.log(xName.replace('Yasinjan', 'GongLi'))
运行结果:
My name is GongLi
2.5 split: 字符串 -> 数组
let xName = 'My name is Yasinjan'
console.log(xName.split('', 5))
运行结果:
[ 'M', 'y', ' ', 'n', 'a' ]