批改状态:合格
老师批语:es6中的类,非常重要, 是基础中的基础,如果学不到, 影响一大片, 所以我们用了足够多的时候来讨论, 你也要多写多看,争取完全彻底掌握
-类
示例
let Person = class{constructor(name){this.name = name;}getName(){return this.name;}}//类的实例化let person = new Person("曹操");console.log(person.getName());
示例图
let createObj = function(className,...args){return new className(...args);};let obj = createObj(class{hello(){return "hello Js";};});console.log(obj.hello());
示例图
示例
let user = new (class{constructor(email){this.email = email}getEmail(){return this.email;}})("qq@qq.com");console.log(user.getEmail());
示例图

class User{constructor(){console.log(this);return Object.create(null);}}const user = new User();console.log(user);class Username{constructor(id,name){this.id = id;this.name = name;this.getName = () => console.log("hello",this.name);}show(){console.log(this.id,this,name);}}const username = new Username(1,"admin");console.log(username);console.log(username.getName());console.log(username.show());
示例图
示例
<script>function User(age){this.age = age;}Object.defineProperty(User.prototype,"verifyAge",{get(){return this.age},set(value){if(value >=18 && value<=60) this.age = value;},configurable:true,enumerable:true,});let user = new User(50);console.log(user);console.log(user.age);console.log(user.verifyAge);user.verifyAge = 80;user.age = 20;console.log(user.age);</script>
示例图
示例
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><script>// 静态成员的特征:// 1.不能被实例访问// 2.不能被实例共享// 3.必须通过类才可以访问//原生function User() {};User.nation = "泰国";User.say = () => `我昨天做梦去了"${user.nation}"哈哈!`;console.dir(User);console.log(User.hasOwnProperty("nation"));console.log(User.hasOwnProperty("say"));console.log(User.hasOwnProperty("constructor"));console.log(User.hasOwnProperty("call"));console.log(User.hasOwnProperty("toString"));// ES6中的类实现静态成员class UserClass{hello(){return "做什么?"}static who = "有个人";static what(){return `${UserClass.who}`;}}console.log(new UserClass().hello());console.log(UserClass.what());</script></body></html>

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>类中的私有成员</title></head><body><script>//1.原生实现私有成员function User(){let course = "ES6编程";let hello = () => `我想我应该好好学习:${course}`;this.say = () =>console.log(hello());}new User().say();//2.es6实现类中的私有成员:使用#号class UserClass{//1.私有属性#salary;constructor(salary = 0) {this.#salary = salary;}get salary(){return this.#salary}set salary(value){if(value< this.salary) console.log("工资太低我不干了");else this.#salary = value;}#max(arr){return Math.max(...arr);}getMax(arr){return this.#max(arr);}static #hobby = '摄影';static #eat(){return "我爱中国!";}static getHobby(){return UserClass.#eat() + "也爱" + UserClass.#hobby;}getHobby(){return UserClass.#hobby;}}const user = new UserClass(8888);console.log(user.salary);user.salary = 6666;user.salary = 9999;console.log(user.salary);console.log(UserClass.getHobby());console.log(new UserClass().getHobby());</script></body></html>
示例图

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>类的继承</title></head><body><script>//原生是通过修改原型链来实现继承//1.父类function Vehicle(fuel,purpose){this.fuel = fuel;this.purpose = purpose;}Vehicle.prototype.show = function(){return `燃料:${this.fuel}\n用途:${this.purpose}\n`;};function Car(fuel,purpose,color){Vehicle.call(this, fuel,purpose);this.color = color;}Car.prototype = Object.create(Vehicle.prototype);Car.prototype.constructor = Car;Car.prototype.show = function(){return Vehicle.prototype.show.call(this).concat(`颜色:${this.color}\n`);}const car = new Car("天然气","商用","红色");console.log(car.show());//ES6中类的继承</script></body></html>
示例图
示例
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><script>class Vehicle{constructor(fuel,purpose){this.fuel = fuel;this.purpose = purpose;}show(){return `燃料:${this.fuel}\n用途:${this.purpose}\n`;}}class Car extends Vehicle{constructor(fuel,purpose,color){//super生成子类的thissuper(fuel,purpose);this.color = color;}show(){return super.show().concat(`颜色:${this.color}\n`);}}const car = new Car("天然气","商用","红色");console.log(car.show());</script></body></html>
示例图
总结:需要多写,看多。要不真的不懂。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号