Blogger Information
Blog 56
fans 1
comment 0
visits 62702
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ES6类的使用
零龙
Original
803 people have browsed it

类的表达式

-类
示例

  1. let Person = class{
  2. constructor(name)
  3. {
  4. this.name = name;
  5. }
  6. getName()
  7. {
  8. return this.name;
  9. }
  10. }
  11. //类的实例化
  12. let person = new Person("曹操");
  13. console.log(person.getName());

示例图

  • 类作为参数使用
    示例
  1. let createObj = function(className,...args)
  2. {
  3. return new className(...args);
  4. };
  5. let obj = createObj(class{
  6. hello(){
  7. return "hello Js";
  8. };
  9. });
  10. console.log(obj.hello());

示例图

  • 立即实例化一个类表达式,创建一个单例

示例

  1. let user = new (class{
  2. constructor(email)
  3. {
  4. this.email = email
  5. }
  6. getEmail()
  7. {
  8. return this.email;
  9. }
  10. })("qq@qq.com");
  11. console.log(user.getEmail());

示例图

类的构造方法

  • 构造方法
  1. class User
  2. {
  3. constructor()
  4. {
  5. console.log(this);
  6. return Object.create(null);
  7. }
  8. }
  9. const user = new User();
  10. console.log(user);
  11. class Username{
  12. constructor(id,name){
  13. this.id = id;
  14. this.name = name;
  15. this.getName = () => console.log("hello",this.name);
  16. }
  17. show()
  18. {
  19. console.log(this.id,this,name);
  20. }
  21. }
  22. const username = new Username(1,"admin");
  23. console.log(username);
  24. console.log(username.getName());
  25. console.log(username.show());

示例图

存储属性

  • 存储属性

示例

  1. <script>
  2. function User(age)
  3. {
  4. this.age = age;
  5. }
  6. Object.defineProperty(User.prototype,"verifyAge",{
  7. get(){return this.age},
  8. set(value)
  9. {
  10. if(value >=18 && value<=60) this.age = value;
  11. },
  12. configurable:true,
  13. enumerable:true,
  14. });
  15. let user = new User(50);
  16. console.log(user);
  17. console.log(user.age);
  18. console.log(user.verifyAge);
  19. user.verifyAge = 80;
  20. user.age = 20;
  21. console.log(user.age);
  22. </script>

示例图

访问静态成员

  • 静态成员

示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 静态成员的特征:
  11. // 1.不能被实例访问
  12. // 2.不能被实例共享
  13. // 3.必须通过类才可以访问
  14. //原生
  15. function User() {};
  16. User.nation = "泰国";
  17. User.say = () => `我昨天做梦去了"${user.nation}"哈哈!`;
  18. console.dir(User);
  19. console.log(User.hasOwnProperty("nation"));
  20. console.log(User.hasOwnProperty("say"));
  21. console.log(User.hasOwnProperty("constructor"));
  22. console.log(User.hasOwnProperty("call"));
  23. console.log(User.hasOwnProperty("toString"));
  24. // ES6中的类实现静态成员
  25. class UserClass
  26. {
  27. hello()
  28. {
  29. return "做什么?"
  30. }
  31. static who = "有个人";
  32. static what()
  33. {
  34. return `${UserClass.who}`;
  35. }
  36. }
  37. console.log(new UserClass().hello());
  38. console.log(UserClass.what());
  39. </script>
  40. </body>
  41. </html>

类中的私有成员

  • 私有成员
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>类中的私有成员</title>
  7. </head>
  8. <body>
  9. <script>
  10. //1.原生实现私有成员
  11. function User()
  12. {
  13. let course = "ES6编程";
  14. let hello = () => `我想我应该好好学习:${course}`;
  15. this.say = () =>console.log(hello());
  16. }
  17. new User().say();
  18. //2.es6实现类中的私有成员:使用#号
  19. class UserClass
  20. {
  21. //1.私有属性
  22. #salary;
  23. constructor(salary = 0) {
  24. this.#salary = salary;
  25. }
  26. get salary()
  27. {
  28. return this.#salary
  29. }
  30. set salary(value){
  31. if(value< this.salary) console.log("工资太低我不干了");
  32. else this.#salary = value;
  33. }
  34. #max(arr){
  35. return Math.max(...arr);
  36. }
  37. getMax(arr){
  38. return this.#max(arr);
  39. }
  40. static #hobby = '摄影';
  41. static #eat(){
  42. return "我爱中国!";
  43. }
  44. static getHobby()
  45. {
  46. return UserClass.#eat() + "也爱" + UserClass.#hobby;
  47. }
  48. getHobby()
  49. {
  50. return UserClass.#hobby;
  51. }
  52. }
  53. const user = new UserClass(8888);
  54. console.log(user.salary);
  55. user.salary = 6666;
  56. user.salary = 9999;
  57. console.log(user.salary);
  58. console.log(UserClass.getHobby());
  59. console.log(new UserClass().getHobby());
  60. </script>
  61. </body>
  62. </html>

示例图

类的继承

  • 原生类的继承
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>类的继承</title>
  7. </head>
  8. <body>
  9. <script>
  10. //原生是通过修改原型链来实现继承
  11. //1.父类
  12. function Vehicle(fuel,purpose){
  13. this.fuel = fuel;
  14. this.purpose = purpose;
  15. }
  16. Vehicle.prototype.show = function(){
  17. return `燃料:${this.fuel}\n用途:${this.purpose}\n`;
  18. };
  19. function Car(fuel,purpose,color){
  20. Vehicle.call(this, fuel,purpose);
  21. this.color = color;
  22. }
  23. Car.prototype = Object.create(Vehicle.prototype);
  24. Car.prototype.constructor = Car;
  25. Car.prototype.show = function(){
  26. return Vehicle.prototype.show
  27. .call(this)
  28. .concat(`颜色:${this.color}\n`);
  29. }
  30. const car = new Car("天然气","商用","红色");
  31. console.log(car.show());
  32. //ES6中类的继承
  33. </script>
  34. </body>
  35. </html>

示例图

  • ES6 类的继承

示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. class Vehicle{
  11. constructor(fuel,purpose){
  12. this.fuel = fuel;
  13. this.purpose = purpose;
  14. }
  15. show()
  16. {
  17. return `燃料:${this.fuel}\n用途:${this.purpose}\n`;
  18. }
  19. }
  20. class Car extends Vehicle{
  21. constructor(fuel,purpose,color){
  22. //super生成子类的this
  23. super(fuel,purpose);
  24. this.color = color;
  25. }
  26. show(){
  27. return super.show().concat(`颜色:${this.color}\n`);
  28. }
  29. }
  30. const car = new Car("天然气","商用","红色");
  31. console.log(car.show());
  32. </script>
  33. </body>
  34. </html>

示例图

总结:需要多写,看多。要不真的不懂。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:es6中的类,非常重要, 是基础中的基础,如果学不到, 影响一大片, 所以我们用了足够多的时候来讨论, 你也要多写多看,争取完全彻底掌握
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post