一、用class创建一个类, 并实现自有,共享,静态成员的声明与输出
...
<script>
...
class demo {
//公共成员
username = '小明';
//构造方法
constructor(name, email){
//自有属性
this.name = name;
this.email = email;
}
//共享成员:原型成员
getInfo(){
return `${this.name}:(${this.email})`
}
//静态成员
static status = 'enable';
}
const obj = new demo('晓东','xiaodong@php.cn');
console.log(obj.getInfo());
console.log(obj.status);
console.log(demo.status);
...
</script>
...
二、实例演示数组与对象解构方法
数组解构
...
<script>
...
const user = ['小明', 'xiaoming@php.cn'];
let username = user[0];
let useremail = user[1];
console.log(username, useremail);
console.log('------数组解构------');
let [username1, useremail1]=['晓东', 'xiaodong@php.cn'];
console.log(username1, useremail1);
console.log('------更新值------');
[username1, useremail1]=['李晓东', 'xiaodong@php.cn'];
console.log(username1, useremail1);
console.log('------参数不足,给默认值。------');
[username1, useremail1, age = 30]=['李晓东', 'xiaodong@php.cn'];
console.log(username1, useremail1, age);
console.log('------参数过多,使用“...”接收------');
[a, b, c, ...arr] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(a, b, c);
console.log(arr);
console.log('------解构用在数值交换------');
let x = 10;
let y = 20;
console.log(x, y);
[y, x] = [x, y];
console.log(x, y);
...
</script>
...
对象解构
...
<script>
...
let {id, lesson, score} = {id:1, lesson:'js', score:88};
console.log(id, lesson, score);
console.log('------更新值------');
({id, lesson, score} = {id:2, lesson:'php', score:98})
console.log(id, lesson, score);
console.log('------命名冲突,起别名。------');
let {id:itemID, num, price} = {id:3, num:10, price:8.8};
console.log(itemID, num, price);
console.log('------类解构应用------');
let user = {id:145, name:'牛牛', email:'niu@php.cn'};
function getUser({id, name, email}){
console.log(id, name, email);
}
getUser(user);
...
</script>
...