Blogger Information
Blog 23
fans 0
comment 3
visits 23656
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Javascript中基础实用知识小汇总
a.
Original
573 people have browsed it

JS 中值传递和引用传递解析

js 变量传递有那些类型?

1. 值传递:原始类型

  • string:字符串
  • number:数值
  • bool:布尔

2. 引用传递:引用类型

  • object 对象
  • array 数组

这两种传递有什么区别?

3. 解析

  • 原始类型的变量值传递是把值赋给另一个变量

如下代码 1 赋值给 a, 再把 a 的值赋给 b,修改 a 的值不会改变 b 的值,这就是值传递

  1. let a = 1;
  2. let b = a;
  3. a = 2;
  4. console.log(a, b);

slA0IO.md.png

  • 引用传递是把访问变量存放数据的内存地址传给另外一个变量,两个变量指向同一个内存地址

如下代码,其实两个变量访问的都是同一个变量内存地址,所以修改一个变量的数据,引用相同内存地址的数据也会直观的看到变化

  1. let arr = [1, 2, 3, 4, 5, 6, 0];
  2. let arr1 = arr;
  3. arr[0] = 9;
  4. console.log(arr[0], arr1[0]);

slEAw6.md.png

那么传递参数又是什么类型?

4. 传参

4.1 原始类型的传参

  • 函数中对参数的更新,并不会影响到入参

slmZDA.md.png

4.2 引用类型的传参

  • 引用类型传递的更新也不会影响入参,所以函数中传参都是值传递

sl8VKK.md.png

4.3 code

  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. let a = 1;
  11. let b = a;
  12. a = 2;
  13. console.log(a, b);
  14. let arr = [1, 2, 3, 4, 5, 6, 0];
  15. let arr1 = arr;
  16. arr[0] = 9;
  17. console.log(arr[0], arr1[0]);
  18. //传递的原始类型时
  19. //函数的传参对于函数内部对变量的更新无影响
  20. const f1 = (x) => (x = 1);
  21. let o = 10;
  22. //入参:调用函数传入的参数
  23. f1(o);
  24. console.log(o);
  25. //传递的是引用类型时
  26. const f2 = (y) => (y.a = 9);
  27. let ob1 = { a: 1, b: 2 };
  28. f2(ob1);
  29. //此时会发现对象中a的值被更新为9,这是为什么呢?
  30. console.log(ob1);
  31. //函数中对于ob1.a的更新生效了,实际上仍是值传递
  32. //对于引用类型,只有全新赋值才算是更新,修改属性不算更新
  33. const ob2 = { a: 10, b: 20 };
  34. ob2.a = 0;
  35. //ob2为一个常量,按理说修改会报错但是是正常的,说明修改对象中的一个属性不算更新
  36. //ob2 = {};
  37. //此时报错
  38. const f3 = (z) => (z = {});
  39. let c = { a: 1, b: 2 };
  40. f3(c);
  41. console.log(c);
  42. //此时可以看到函数内对引用类型传递的更新也不会影响入参,所以函数中传参都是值传递
  43. </script>
  44. </body>
  45. </html>

数组与对象的解构赋值怎么做

用途:快速从集合数据(数组/对象)解构出独立变量

1. 数组

  • 数组支持给变量添加默认值
  1. let [a, b, c] = [1, 2, 3];
  2. console.log(a, b, c);
  3. //支持默认值
  4. [a, b, c, d = "XXX"] = [1, 2, 3];

2. 对象

  1. let { name, id } = { id: "a100", name: "手机" };
  2. console.log(name);
  • 对象解构赋值支持别名,避免同名变量被更新
  1. let email = "admin@php.cn";
  2. ({ user, email } = { user: "name", email: "cp@php.cn" });
  3. console.log(email);
  4. //此时我们并不想更新email变量,可以给变量起一个别名
  5. email = "admin@php.cn";
  6. ({ user, email: useremail } = { user: "name", email: "cp@php.cn" });
  7. console.log(useremail);
  8. console.log(email);

3.传参同样可以用解构赋值

  1. //数组传参
  2. let sum = ([a, b]) => a + b;
  3. console.log(sum([10, 11]));
  4. //对象传参
  5. let ar = ({ c, d }) => [c, d];
  6. console.log(ar({ c: "我是C", d: "我是D" }));

4. code

slc4KO.md.png

  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. let [a, b, c] = [1, 2, 3];
  13. console.log(a, b, c);
  14. //支持默认值
  15. [a, b, c, d = "XXX"] = [1, 2, 3];
  16. //2.对象
  17. let { name, id } = { id: "a100", name: "手机" };
  18. console.log(name);
  19. //3.对象解构赋值支持别名,避免同名变量被更新
  20. //这里声明一个与对象解构变量同名的变量
  21. let email = "admin@php.cn";
  22. ({ user, email } = { user: "name", email: "cp@php.cn" });
  23. console.log(email);
  24. //此时我们并不想更新email变量,可以给变量起一个别名
  25. email = "admin@php.cn";
  26. ({ user, email: useremail } = { user: "name", email: "cp@php.cn" });
  27. console.log(useremail);
  28. console.log(email);
  29. //参数解构
  30. //数组传参
  31. let sum = ([a, b]) => a + b;
  32. console.log(sum([10, 11]));
  33. //对象传参
  34. let ar = ({ c, d }) => [c, d];
  35. console.log(ar({ c: "我是C", d: "我是D" }));
  36. </script>
  37. </body>
  38. </html>

call,apply,bind 是什么?

作用是改变函数运行时 this 的指向

slgkR0.md.png

code

  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. <button>click</button>
  10. <script>
  11. function hello(name) {
  12. this.name = name;
  13. console.log(this.name);
  14. }
  15. const obj = { name: "admin" };
  16. //经典调用
  17. hello("朱老师");
  18. //bind()不会立即执行,只返回一个函数声明
  19. let f = hello.bind(obj, "bind");
  20. console.log(f);
  21. f();
  22. //call/apply立即执行
  23. f = hello.call(obj, "call");
  24. console.log(f);
  25. // apply要求第二个参数必须是数组
  26. f = hello.apply(obj, ["apply"]);
  27. console.log(f);
  28. //bind()用处
  29. //动态改变this指向
  30. document.querySelector("button").addEventListener(
  31. "click",
  32. function () {
  33. console.log(this);
  34. console.log(this.name);
  35. }.bind({ name: "bind啊" })
  36. );
  37. </script>
  38. </body>
  39. </html>

javascript 访问器原理与实现

1. 访问器原理就是用set,get将方法伪造成一个属性

2. get用于访问 set用于传值

3. 代码演示

slgfFs.md.png

  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. const product = {
  11. data: [
  12. { name: "电脑", price: 5000, num: 5 },
  13. { name: "手机", price: 4000, num: 9 },
  14. { name: "相机", price: 5500, num: 1 },
  15. ],
  16. getAmounts() {
  17. return this.data.reduce((t, c) => (t += c.price * c.num), 0);
  18. },
  19. //将方法伪造成一个属性
  20. get getprice() {
  21. return this.data.reduce((t, c) => (t += c.price * c.num), 0);
  22. },
  23. //设置价格
  24. set setprice(price) {
  25. this.data[1].price = price;
  26. },
  27. };
  28. //普通方式访问
  29. console.log(product.getAmounts());
  30. //访问器方法访问
  31. console.log(product.getprice);
  32. //设置价格
  33. product.setprice = 200;
  34. console.log(product.getprice);
  35. //访问器优先级高于普通属性
  36. let us = {
  37. name: "朱老师",
  38. get name() {
  39. return "不是朱老师";
  40. },
  41. set name(value) {
  42. this.name = value;
  43. },
  44. };
  45. console.log(us.name);
  46. //解决同名优先级问题
  47. let us1 = {
  48. data: { name: "朱老师" },
  49. get name() {
  50. return this.data.name;
  51. },
  52. set name(value) {
  53. this.data.name = value;
  54. },
  55. };
  56. us1.name = "000";
  57. console.log(us1.name);
  58. </script>
  59. </body>
  60. </html>

javascript 分支 与三元运算

分支用于逻辑判断,让代码有了灵魂

  • if...else

常用区间判断

  1. let score = 81;
  2. //单分支
  3. if (score >= 60) {
  4. console.log("及格");
  5. }
  6. //双分支
  7. if (score >= 60) {
  8. console.log("及格 ");
  9. } else {
  10. console.log("不及格");
  11. }
  12. //多分支
  13. score = 101;
  14. if (score > 100 || score < 0) {
  15. console.log("成绩不合法");
  16. } else if (score >= 60 && score < 80) {
  17. console.log("合格");
  18. } else if (score >= 80 && score <= 100) {
  19. console.log("优秀");
  20. } else {
  21. console.log("不及格");
  22. }
  • switch

一般用于单值判断

  1. score = 19;
  2. //switch 来简化分支
  3. //多值区间断送
  4. switch (true) {
  5. case score > 100 || score < 0:
  6. console.log("成绩不合法");
  7. break;
  8. case score >= 60 && score < 80:
  9. console.log("合格");
  10. break;
  11. case score >= 80 && score <= 100:
  12. console.log("优秀");
  13. break;
  14. default:
  15. console.log("不及格");
  16. }
  17. //switch 单值判断
  18. let response = "success";
  19. switch (response.toLowerCase()) {
  20. case "fail":
  21. console.log("请求失败");
  22. break;
  23. case "success":
  24. console.log("请求成功");
  25. break;
  26. default:
  27. console.log("未知错误 ");
  28. }
  • 条件 ? true : false 用于二分支运算

三元运算简化了代码,但是只有用于二分支

  1. // 语法:条件 ? true : false
  2. console.log(score >= 60 ? "及格" : "不及格 ");

code

  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>javascript分支</title>
  7. </head>
  8. <body>
  9. <script>
  10. let score = 81;
  11. //单分支
  12. if (score >= 60) {
  13. console.log("及格");
  14. }
  15. //双分支
  16. if (score >= 60) {
  17. console.log("及格 ");
  18. } else {
  19. console.log("不及格");
  20. }
  21. //多分支
  22. score = 101;
  23. if (score > 100 || score < 0) {
  24. console.log("成绩不合法");
  25. } else if (score >= 60 && score < 80) {
  26. console.log("合格");
  27. } else if (score >= 80 && score <= 100) {
  28. console.log("优秀");
  29. } else {
  30. console.log("不及格");
  31. }
  32. score = 19;
  33. //switch 来简化分支
  34. //多值区间断送
  35. switch (true) {
  36. case score > 100 || score < 0:
  37. console.log("成绩不合法");
  38. break;
  39. case score >= 60 && score < 80:
  40. console.log("合格");
  41. break;
  42. case score >= 80 && score <= 100:
  43. console.log("优秀");
  44. break;
  45. default:
  46. console.log("不及格");
  47. }
  48. //switch 单值判断
  49. let response = "success";
  50. switch (response.toLowerCase()) {
  51. case "fail":
  52. console.log("请求失败");
  53. break;
  54. case "success":
  55. console.log("请求成功");
  56. break;
  57. default:
  58. console.log("未知错误 ");
  59. }
  60. //三元运算
  61. // 语法:条件 ? true : false
  62. console.log(score >= 60 ? "及格" : "不及格 ");
  63. </script>
  64. </body>
  65. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:很认真
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