1.用class实现自有成员,共享成员,静态成员的声明和输出!
class US2 {
// 构造方法,初始化
constructor(name, num) {
// 自有成员
this.name = name;
this.num = num;
}
// 共享成员
getInfo() {
return `${this.name} : (${this.num})`;
}
// 静态成员
static static = "6666";
}
// 自有实例
let us1 = new US2("老王111", 10);
let us2 = new US2("老王222", 10);
console.log(us1, us2);
// 共享实例
console.log(us1.getInfo(), us2.getInfo());
// 静态实例
console.log(US2.static);
2.数组和对象的解构方法
1.数组解构
let [aa, bb] = ["你好啊", "我很好"];
console.log(aa, bb);
// 更新
[aa, bb] = ["hello", "world"];
console.log(aa, bb);
// 参数不足:给默认值
[aa, bb, cc = "笨蛋"] = ["hello", "world"];
console.log(aa, bb, cc);
// 参数过多:...rest
[a, b, ...arr] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
console.log(a, b);
console.log("----------------");
console.log(arr);
console.log("----------------");
console.log(a, b, ...arr);
2.对象结构
let { id, jg, num } = { id: 1, jg: 2000, num: 3 };
console.log(id, jg, num);
// 更新
({ id, jg, num } = { id: 100, jg: 5000, num: 20 });
console.log(id, jg, num);
// 命名冲突,起别名
({ id: id2, jg1, num1 } = { id: 10, jg1: 500, num1: 200 });
console.log(id2, jg1, num1);
// 演示案例
let user = { id: 1, jg: 880, num: 3 };
function getUser({ id, jg, num }) {
console.log(id, jg, num);
}
getUser(user);