亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 56
粉絲 1
評論 0
訪問量 76334
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
ES6類的使用
零龍
原創(chuàng)
1035人瀏覽過

類的表達(dá)式

-類
示例

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

示例圖

  • 類作為參數(shù)使用
    示例
  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());

示例圖

  • 立即實(shí)例化一個類表達(dá)式,創(chuàng)建一個單例

示例

  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());

示例圖

類的構(gòu)造方法

  • 構(gòu)造方法
  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>

示例圖

訪問靜態(tài)成員

  • 靜態(tài)成員

示例

  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. // 靜態(tài)成員的特征:
  11. // 1.不能被實(shí)例訪問
  12. // 2.不能被實(shí)例共享
  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中的類實(shí)現(xiàn)靜態(tài)成員
  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.原生實(shí)現(xiàn)私有成員
  11. function User()
  12. {
  13. let course = "ES6編程";
  14. let hello = () => `我想我應(yīng)該好好學(xué)習(xí):${course}`;
  15. this.say = () =>console.log(hello());
  16. }
  17. new User().say();
  18. //2.es6實(shí)現(xiàn)類中的私有成員:使用#號
  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. //原生是通過修改原型鏈來實(shí)現(xiàn)繼承
  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>

示例圖

總結(jié):需要多寫,看多。要不真的不懂。

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語:es6中的類,非常重要, 是基礎(chǔ)中的基礎(chǔ),如果學(xué)不到, 影響一大片, 所以我們用了足夠多的時候來討論, 你也要多寫多看,爭取完全徹底掌握
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)