Blogger Information
Blog 25
fans 0
comment 0
visits 9452
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
前端开发-演标class类与extends,super,字符串,数组API等用法20221104
P粉114035831
Original
411 people have browsed it

演标class类与extends,super等用法

let User = class {

构造函数:声明属性

constructor(uname,email){
// (1)属性
this.uname = uname
this.email = email
}
//(2)方法
say(){
return ${this.uname}:(${this.email})
}
//(3)静态成员
static nation = ‘CHINA’
}

类的实例化

// let User =new User(‘李小姐’,’asd@qq.com’)
const user = new User(‘李小姐’,’asd@qq.com’)
console.log(user,say)
console.log(“———————-“)

继承

class child extends User {
constructor(uname,email,sex){
super(uname,email)
this.sex =sex
}

  1. say () {
  2. //super.say()父类中的say()
  3. return `${super.say()},(${this.sex})`
  4. }

}

const child =new Child (‘王小姐’,’asd@qq.com’,’女’)
console.log(child.say())
console.log(‘——————————-‘)

演示字符串

let str = ‘PHP中文网’
// 0 ->p
// 1 ->h
// 2 ->p
// 3 ->中
// 4 ->文
// 5 ->网

1.length 属性

console.log(‘length= ‘,str.length)

2.charAt(): 索引 ->成员

//把要索引的位数引入则可得
console.log(str[5])

3.indexOf(): 成员 ->索引

console.log(str.indexOf(‘中’))

4.replace()替换

// ()网的左右字符串替换
console.log(str.replace(‘中文网’,’.cn’))

5.split:字符串 -> 数组

//把前面三个转成数组
console.log(str.split(‘’,3))

6.大小写

console.log(‘PHP.CN’.toLocaleLowerCase())
console.log(‘www.php.cn’.toUpperCase())
console.log(‘—————————-‘)

演示数组API

1. 建一个变量,压缩数组表达

let n = [1,2,3]
let n1 = […n]
console.log(n1)
// 更新.展示数组表达
n = […n,4,5]
console.log(n)
console.log(‘———————‘)

2.Array.of 打包表达.首个字母为大写

let f = [1,2,3,4,5,6]
console.log(Array.of(…f))
console.log(‘———————‘)

3. Array.from(): 类数组(对象)包装转为一个真正的数组。方便使用

const likeArr = {
0:’d’,
1:’s’,
2:’h’,
length:3,
}

const data = Array.from(likeArr)
// for-of关注值
for (let item of data) {
console.log(item)
}
console.log(‘———————‘)

4.增删元素

let arr = []
//从尾部添加
console.log(arr.push(10))
console.log(arr.pop())
//从头部添加
console.log.apply(arr.unshift(1))
console.log(arr.shift())
//删除数组
arr1 = [1,2,3,4]
//删除第二个数组(2)
delete arr1 [1]
console.log(arr1)
console.log(‘———————‘)

5.迭代

let arr2 = [1,2,3,4,5]
//forEach 没有返回值
arr2.forEach(function(item,key,arr){
console.log(item,key,arr)
})
//map 有返回
let result = arr2.map(function(item,key,arr){
return item
// return key
// return arr
})
console.log(result)

6.reduce 归并

arr3 = [1, 2, 3, 4, 5]
result = arr3.reduce(function (acc,cur,key,arr3) {
console.log(acc=${acc},cur=${cur},key=${key},arr3=[${arr3}])
return acc + cur
},0)
console.log(result)

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