1.对象字面量
<script>
// 1.对象字面量
let item = {
data:{
name:'asd',
price:5000
},
getPrice(){
return this.data.price;
},//接口函数
setPrice(x){
this.data.price = x;
}
}
console.log(item.data.price);
console.log(item.getPrice());
// 追加字面量
let student = {}
student.x = 100;
student.y = 200;//设置变量
student.getPrice = function (x){
this.y = x;
}//设置函数
// console.log(student);
student.getPrice(300);
console.log(student);
</script>
总结:对象字面量,要么一次性设置全部属性,要么
一个一个添加;
<script>
//访问器属性
let student={
data:{
name:'ajs',
price:200
},
name(x){
this.data.name = x
},
name(){
return this.data.name;
}
}
// 访问器属性
console.log(student.name());
</script>
2.构造函数
<script>
//构造函数一定是function(){}
let user = function(name,price){
this.name = name;
this.price = price;
this.getValue = () => {
console.log(this.name);
};
};
// user.prototype.getValue = () =>{
// console.log(`${this.name}:${this.value}`);
// }
let User1 = new user('as',200);
User1.getValue();
</script>
继承:
<script>
//继承,构造函数都用function(){}
let Parent = function(){};
Parent.prototype.sum = function() {
console.log(this.a);
};
let Child = function (a, b) {
this.a = a;
this.b = b;
};
Child.prototype = Parent.prototype;
let child = new Child(100, 200);
child.sum();
</script>
3.类
//申明类
class student{
constructor(name,value){
this.name = name;
this.value = value;
};
getSum(){
console.log(`${this.value}`);
};
} ;
let student1 = new student('12',23);
student1.getSum();
//继承
class teacher extends student{
constructor(name,value,sex){
super(name,value);
this.sex = sex;
}
getx(){
console.log(this.sex);
}
}
let teachers = new teacher('12',23,34);
</script>
4.结构赋值
4.1数组解构
<script>
//数组解构
let [a,b,c] = [1,2,3];
console.log(a,b,c);
let [x,...y] = [1,2,3,4,5];
console.log(x,y);
</script>
4.2对象解构
<script>
function f({name,value})
{
console.log(name,value);
};
let student={
name:'as',
value:120
};
f(student);
</script>