批改状态:合格
老师批语:构造函数名一般使用首字母大写形式
<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>
<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>
//申明类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>
<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>
<script>function f({name,value}){console.log(name,value);};let student={name:'as',value:120};f(student);</script>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号