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

搜索
博主信息
博文 18
粉絲 0
評論 0
訪問量 14946
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
210401 JavaScript 值傳遞與引用傳遞, 模板字面量, 標(biāo)簽函數(shù), 解構(gòu), bind() call() apply() 函數(shù), Getter Setter, 流程控制
xyphpblog
原創(chuàng)
715人瀏覽過

1. 值傳遞與引用傳遞

1.1 值傳遞

  • 值傳遞發(fā)生在基本數(shù)據(jù)類型中
  1. let a = 10;
  2. let b = a;
  3. console.log(a, b); //10 10
  4. a = 5;
  5. console.log(a, b); //5 10

1.2 引用傳遞

  • 發(fā)生在引用類型,數(shù)組,對象
  1. let s1 = {
  2. x: 1,
  3. y: 1
  4. };
  5. console.log(s1);
  6. // {x:1, y:1}
  7. let s2 = s1;
  8. console.log(s2);
  9. // {x:1, y:1}
  10. console.log(s1 === s2);
  11. // true
  12. s1.x = 10;
  13. console.log(s1);
  14. // {x:10, y:1}
  15. console.log(s2);
  16. // {x:10, y:1}

1.3 傳參

  • 函數(shù)中,始終是值傳遞
  • 傳入對象,更新對象的屬性仍屬于值傳遞,因?yàn)閷ο鬀]有被改變
  1. //基本類型
  2. const f1 = x => x = 10;
  3. let p = 5;
  4. f1(p);
  5. console.log(p);
  6. //對象
  7. const f2 = (obj) => { obj.age = 22; obj = {}; };
  8. let o = {
  9. age: 10,
  10. id: 0001
  11. }
  12. console.log(o);
  13. // {age:10, id:1}
  14. f2(o);
  15. console.log(o);
  16. // {age:22, id:1}

*

  • 深拷貝:值傳遞
  • 淺拷貝:引用傳遞

2. 模板字面量

  • 字符串字面量+變量插值(嵌入到字符串)
  1. let name = "Tom";
  2. let greet = `Hello, ${name}`;
  3. console.log(greet);
  4. let stu = {
  5. id: 1112,
  6. name: "Em",
  7. age: 25,
  8. married: false
  9. }
  10. let stuInfo = `
  11. <div>
  12. <p>ID: ${stu.id}</p>
  13. <p>姓名:${stu.name}</p>
  14. <p>年齡:${stu.age}</p>
  15. <p>已婚:${stu.married}</p>
  16. </div>
  17. `;
  18. console.log(stuInfo);
  19. document.write(stuInfo);

2.1 標(biāo)簽函數(shù)

  • 第一個(gè)參數(shù):字符串本身元素組成的數(shù)組
  • 第二個(gè)及以后:插值/插值組成的數(shù)組
  1. //少量參數(shù)
  2. let sum = (strs, a, b) => {
  3. console.log(strs);
  4. console.log(a, b);
  5. }
  6. let a = 5;
  7. let b = 10;
  8. let c = 20;
  9. sum`${a}+${b}=`;
  10. //多個(gè)參數(shù)
  11. sum = (strs, ...arr) => {
  12. console.log(strs);
  13. console.log(arr);
  14. }
  15. sum`${a}+${b}+${c}=`;

3. 解構(gòu)

3.1 數(shù)組解構(gòu)

  1. let arr = [1, 2, 3];
  2. let [a, b] = arr;
  3. console.log(a, b);
  4. // 1 2
  5. let c;
  6. [a, b, c] = arr;
  7. console.log(c);
  8. // 3
  9. arr = [1, 2, 3, 4, 5, 6, 7];
  10. [a, b, ...c] = arr;
  11. console.log(a, b, ...c);
  12. // 1 2 3 4 5 6 7
  13. [, , , , , c,] = arr;
  14. console.log(c);
  15. // 6
  16. // let [x, y] = [8, 9];
  17. x = 80;
  18. y = 90;
  19. console.log(x, y);
  20. // 80 90
  21. [y, x] = [x, y];
  22. console.log(x, y)
  23. // 90 80

3.2 對象解構(gòu)

  1. let stu = { name: "Tom", age: 22 };
  2. ({ name, age } = stu);
  3. // let { name, age } = stu;
  4. console.log(name, age)
  5. // Tom 22

3.3 參數(shù)解構(gòu)

  1. let sum = ([a, b]) => a + b;
  2. console.log(sum([60, 60]));
  3. // 120
  4. let stu1 = ({ name, age }) => [name, age];
  5. console.log(stu1({ name: "Em", age: 25 }));
  6. //["Em", 25]

3.4 對象簡化

  • 如果對象的屬性與變量作用域相同且同名,可以寫作:
  1. x = 90, y = 80;
  2. let user = {
  3. x,
  4. y
  5. }
  6. console.log(user);
  7. // {x: 90, y: 80}

4 bind() call() apply() 函數(shù)

  • 默認(rèn)的this關(guān)鍵字指向window
  • 在對象中作為屬性的函數(shù)里,this指向的是當(dāng)前的對象
  • 這三個(gè)函數(shù)用來改變this關(guān)鍵字的指向
  1. let stu = {
  2. name: "Em",
  3. age: 30
  4. }
  5. let name = "hehe";
  6. let obj = {
  7. name: "Tom",
  8. age: 22,
  9. foo: function () {
  10. console.log("Name: " + this.name);
  11. console.log("Age: " + this.age);
  12. }
  13. }
  14. console.log(this.name);
  15. obj.foo();
  16. obj.foo.bind(stu)();
  17. obj.foo.call(stu);
  18. obj.foo.apply(stu);

  • 若函數(shù)有參數(shù)需傳入:
  1. obj = {
  2. name: "Tom",
  3. age: 22,
  4. foo: function (i, s) {
  5. console.log("Name: " + this.name);
  6. console.log("Age: " + this.age);
  7. console.log("Interest: " + i);
  8. console.log("Sport: " + s);
  9. }
  10. }
  11. obj.foo.bind(stu, "Xbox", "Tennis")();
  12. obj.foo.apply(stu, ["PS4", "Badminton"]);
  13. obj.foo.call(stu, "Switch", "Basketball");

5. 訪問器屬性 Getter Setter

  • 方法以屬性的方式使用
  • 簡化語法
  1. let item = {
  2. name: "unknown",
  3. price: 0,
  4. amount: 0,
  5. set setName(name) {
  6. this.name = name;
  7. },
  8. set setPrice(price) {
  9. this.price = price;
  10. },
  11. get addItem() {
  12. this.amount++;
  13. },
  14. get getName() {
  15. return this.name;
  16. },
  17. get getPrice() {
  18. return this.price;
  19. },
  20. get getAmount() {
  21. return this.amount;
  22. }
  23. }
  24. item.addItem;
  25. console.log(item.getAmount);
  26. item.setName = "iPad";
  27. console.log(item.getName);
  28. item.setPrice = "199";
  29. console.log(item.getPrice);
  30. //1
  31. //iPad
  32. //199

6. 流程控制

6.1 if 語句

  • 語法
    1. if(true) return a = 1;
    2. else return a = 2;
  • 當(dāng)只有執(zhí)行語句只有一行時(shí),{}括號可以省略,多行時(shí)不可以
  1. let a = 1;
  2. if (a > 60)
  3. console.log(true);
  4. else
  5. console.log(false);
  6. // false
  • 多個(gè)分支時(shí)
  1. let foo = (speed) => {
  2. if (speed < 60 && speed >= 0) {
  3. console.log("too slow");
  4. } else if (speed >= 60 && speed <= 100) {
  5. console.log("just right");
  6. } else if (speed > 100 && speed < 200) {
  7. console.log("too fast");
  8. } else {
  9. console.log("???");
  10. }
  11. }
  12. let currentSpeed = 80;
  13. foo(currentSpeed);
  14. currentSpeed = 30;
  15. foo(currentSpeed);
  16. currentSpeed = 150;
  17. foo(currentSpeed);
  18. currentSpeed = -1;
  19. foo(currentSpeed);

6.2 switch 語句

  • 語法
    1. switch(true) {
    2. case a:
    3. ...;
    4. break;
    5. case b:
    6. ...;
    7. break;
    8. default:
    9. ...;
    10. break;
    11. }

*case 也可以是表達(dá)式表示多個(gè)值,如:

case a>0&&a<10:
console.log(true);

但多用于單個(gè)值的判斷

  1. let animal = "cat";
  2. switch (animal) {
  3. case "cat":
  4. console.log("貓");
  5. break;
  6. case "dog":
  7. console.log("狗");
  8. break;
  9. default:
  10. break;
  11. }
  12. //貓

6.3 三元運(yùn)算符

  • 用于簡化雙分支
  1. currentSpeed = 2000;
  2. animal = "cat";
  3. console.log(currentSpeed <= 200 ? "OK" : "Boom");
  4. //Boom
  5. console.log(animal === "cat" ? true : false);
  6. //true
批改老師:天蓬老師天蓬老師

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

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(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
隨時(shí)隨地碎片化學(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é)