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

搜索
博主信息
博文 18
粉絲 0
評論 0
訪問量 14929
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
210402 JavaScript 作用域 閉包 原型 構(gòu)造函數(shù)
xyphpblog
原創(chuàng)
637人瀏覽過

1. 作用域

  • 全局
  • 函數(shù)

1.1 全局作用域

  1. <script>
  2. let a = 1;
  3. console.log(a);
  4. //1
  5. </script>

1.2 函數(shù)作用域

  1. let a = 1;
  2. //console.log(a);
  3. function foo() {
  4. let a = 5;
  5. return a;
  6. }
  7. console.log(foo());
  8. //5

1.3 塊作用域

  • 只有在塊作用域中使用let或const聲明才有效,使用var無效(作用域會提升)
  1. {
  2. let m = 8;
  3. const M = 9;
  4. }
  5. console.log(M);
  6. console.log(m);
  7. //均會報錯

2. 閉包

  • 用來訪問函數(shù)內(nèi)部的局部變量
  1. function foo() {
  2. let a = 0;
  3. return function f(params) {
  4. return a;
  5. }
  6. }
  7. let f = foo();
  8. console.log(f());

3. 循環(huán)

  • 用于遍歷數(shù)組,對象

3.1 while loop

  • 數(shù)組
  1. const arr = ["cat", "dog", "pig"];
  2. let i = 0;
  3. while (i < arr.length) {
  4. console.log(`animal: ${arr[i]}`);
  5. i += 1;
  6. }
  7. //animal: cat
  8. //animal: dog
  9. //animal: pig

3.2 do…while loop

  • 數(shù)組
  1. let i = 1;
  2. do {
  3. i += 1;
  4. console.log(`animal: ${arr[i]}`);
  5. } while (i < arr.length - 1);
  6. //animal: pig

3.3 for…in

  • 對象
  1. const stu = {
  2. id: 1,
  3. name: "Jack",
  4. gender: "male",
  5. graduate: false
  6. }
  7. for (const key in stu) {
  8. console.log("%c%s", "color:green", stu[key]);
  9. }
  10. //1
  11. //Jack
  12. //male
  13. //false

3.3 迭代器

  1. for (const item of arr) {
  2. console.log(item);
  3. }
  4. //animal: cat
  5. //animal: dog
  6. //animal: pig

4. 構(gòu)造函數(shù)和原型

JavaScript 常被描述為一種基于原型的語言 (prototype-based language)——每個對象擁有一個原型對象,對象以其原型為模板、從原型繼承方法和屬性。原型對象也可能擁有原型,并從中繼承方法和屬性,一層一層、以此類推。這種關(guān)系常被稱為原型鏈 (prototype chain),它解釋了為何一個對象會擁有定義在其他對象中的屬性和方法。

準(zhǔn)確地說,這些屬性和方法定義在Object的構(gòu)造器函數(shù)(constructor functions)之上的prototype屬性上,而非對象實例本身。

  • 函數(shù)本身是對象,其中一個屬性為prototype
  • 可作為對象的構(gòu)造器,構(gòu)造函數(shù)
  • javascript沒有“類”的概念,都是通過原型來實現(xiàn)繼承

4.1 構(gòu)造函數(shù)

  • 在函數(shù)中不再創(chuàng)建對象并返回
  • 命名一般以大寫字母開頭,以區(qū)分構(gòu)建函數(shù)和普通函數(shù)
  • 函數(shù)作為構(gòu)造函數(shù)使用時,必須使用new關(guān)鍵字
  • 類的實例化:通過構(gòu)造函數(shù)創(chuàng)建對象的過程(此時構(gòu)造函數(shù)可看成類)
  1. function User(name, age) {
  2. this.name = name;
  3. this.age = age;
  4. this.info = function () {
  5. return { name: this.name, age: this.age };
  6. }
  7. }

4.2 原型

  • 每一個函數(shù)的特殊屬性:prototype
  • 原型函數(shù)的實例的proto屬性,相當(dāng)于原型函數(shù)的prototype屬性
  • 每個實例對象都從原型中繼承了一個constructor屬性,該屬性指向了用于構(gòu)造此實例對象的構(gòu)造函數(shù)。
  1. function User(params) {
  2. this.p = params;
  3. }
  4. const user = new User("hehe");
  5. console.log(user);
  6. //user對象的原型
  7. console.log(user.__proto__);
  8. //user構(gòu)造函數(shù)的原型
  9. console.log(User.prototype);
  10. console.log(user.__proto__ === User.prototype);

原型鏈

  1. 當(dāng)訪問原型函數(shù)實例的屬性時,會先檢查該實例是否具有要訪問的屬性。
  2. 如果 實例 沒有這個屬性, 然后瀏覽器就會在 實例 的 proto 中查找這個屬性(也就是 原型函數(shù)的.prototype)
  3. 如果 實例 的 proto 有這個屬性, 那么它的 proto 上的這個屬性就會被使用.
  4. 否則, 如果 實例 的 __proto 沒有這個屬性, 瀏覽器就會去查找它的 protoproto ,看它是否有這個屬性.
  5. 默認情況下, 所有函數(shù)的原型屬性的 proto 就是 window.Object.prototype.
  6. 所以 實例 的 protoproto (也就是 原型函數(shù)的.prototype 的 proto (也就是 Object.prototype)) 會被查找是否有這個屬性.
  7. 如果沒有在它里面找到這個屬性, 然后就會在 實例 的 protoprotoproto 里面查找.
  8. 然而這有一個問題: 實例 的 protoprotoproto 不存在.
  9. 最后, 原型鏈上面的所有的 proto 都被找完了, 瀏覽器所有已經(jīng)聲明了的 proto 上都不存在這個屬性
  10. 然后就得出結(jié)論,這個屬性是 undefined.

常見定義做法:

  • 在構(gòu)造函數(shù)內(nèi)定義屬性
  • 在其prototype屬性上定義方法
  1. function User(name, age) {
  2. this.name = name;
  3. this.age = age;
  4. // this.info = function () {
  5. // return { name: this.name, age: this.age };
  6. // }
  7. }
  8. //將info()放到原型prototype屬性中
  9. User.prototype.info = function () {
  10. return { name: this.name, age: this.age };
  11. };
  12. //生成對象
  13. const user = new User("Tom", 23);
  14. console.log(user);
  15. //User {name: "Tom", age: 23}
  16. console.log(user.info());
  17. //{name: "Tom", age: 23}

5. 類與繼承

  • 子類繼承父類的屬性
  • 可以重寫父類方法
  • 可以有新的屬性
  • 不能繼承父類的私有屬性
  1. class Animal {
  2. //構(gòu)造方法
  3. constructor(name, leg) {
  4. this.name = name;
  5. this.leg = leg;
  6. }
  7. //原型方法
  8. info() {
  9. return { name: this.name, leg: this.leg, isPet: this.#isPet };
  10. }
  11. //靜態(tài)方法
  12. static eat() {
  13. return "food";
  14. }
  15. //靜態(tài)屬性
  16. static nature = "creature";
  17. //私有屬性
  18. #isPet = false;
  19. //訪問器方法 getter, setter
  20. get isPet() {
  21. return this.#isPet;
  22. }
  23. set isPet(value) {
  24. this.#isPet = value;
  25. }
  26. }
  27. const cat = new Animal("Cat", 4);
  28. const bird = new Animal("Bird", 2);
  29. console.log(cat.info());
  30. //{name: "Cat", leg: 4, isPet: false}
  31. console.log(bird.info());
  32. //{name: "Bird", leg: 2, isPet: false}
  33. console.log(`動物吃:${Animal.eat()}`);
  34. //動物吃:food
  35. console.log(`動物本身是:${Animal.nature}`);
  36. //動物本身是:creature
  37. console.log(`貓是寵物嗎? ${cat.isPet}`);
  38. //貓是寵物嗎? false
  39. cat.isPet = true;
  40. console.log(`貓是寵物嗎? ${cat.isPet}`);
  41. //貓是寵物嗎? true
  42. //繼承
  43. class Dog extends Animal {
  44. //繼承
  45. //第一步必須執(zhí)行父類構(gòu)造方法,否則無法使用this
  46. constructor(name, type) {
  47. super(name, 4);
  48. //新成員初始化
  49. this.type = type;
  50. }
  51. info() {
  52. return { name: this.name, leg: this.leg, type: this.type };
  53. }
  54. //父類的私有屬性不會被繼承
  55. }
  56. const dog = new Dog("Dog", "哈士奇");
  57. console.log(dog.info());
  58. //{name: "Dog", leg: 4, type: "哈士奇"}
批改老師:天蓬老師天蓬老師

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

老師批語:
本博文版權(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+教程免費學(xué)