模板字符串

模板字符串是什么

认识模板字符串

  1. const username1 = 'alex';
  2. // "alex"
  3. const username2 = `alex`;
  4. console.log(username1, username2, username1 === username2);

模板字符串与一般字符串的区别

  1. const person = {
  2. username: 'Alex',
  3. age: 18,
  4. sex: 'male'
  5. };
  6. const info =
  7. '我的名字是:' +
  8. person.username +
  9. ', 性别:' +
  10. person.sex +
  11. ', 今年' +
  12. person.age +
  13. '岁了';
  14. console.log(info);
  15. const inf = `我的名字是:${person.username}, 性别:${person.sex}, 今年${person.age}岁了`;
  16. console.log(inf);
  17. //和其他东西一起使用的时候,使用模板字符串,方便注入
  18. // 其他情况下使用模板字符串或一般字符串都行

模板字符串的注意事项

输出多行字符串

  1. // 一般字符串
  2. // const info = '第1行\n第2行';
  3. // console.log(info);
  4. // 模板字符串
  5. //const info = `第1行\n第2行`;
  6. const info = `第1
  7. 2行`;
  8. console.log(info);
  9. // 模板字符串中,所有的空格、换行或缩进都会被保留在输出之中

输出`和\等特殊字符

  1. const info = `'\`\\`;
  2. const inf=`'\``;
  3. console.log(info);
  4. console.log(inf);

模板字符串的注入

  1. // ${}
  2. const username = 'alex';
  3. const person = { age: 18, sex: 'male' };
  4. const getSex = function (sex) {
  5. return sex==='male'?'nan':'nv';
  6. };
  7. const info = `${username}, ${person.age + 2}, ${getSex(person.sex)}`;
  8. console.log(info);
  9. // 只要最终可以得出一个值的就可以通过 ${} 注入到模板字符串中

模板字符串的应用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>模板字符串的应用</title>
  6. <style>
  7. body {
  8. padding: 50px 0 0 300px;
  9. font-size: 22px;
  10. }
  11. ul {
  12. padding: 0;
  13. }
  14. p {
  15. margin-bottom: 10px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <p>学生信息表</p>
  21. <ul id="list">
  22. <li style="list-style: none;">信息加载中……</li>
  23. </ul>
  24. <script>
  25. // 数据
  26. const students = [
  27. {
  28. username: 'Alex',
  29. age: 18,
  30. sex: 'male'
  31. },
  32. {
  33. username: 'ZhangSan',
  34. age: 28,
  35. sex: 'male'
  36. },
  37. {
  38. username: 'LiSi',
  39. age: 20,
  40. sex: 'female',
  41. se:'s'
  42. }
  43. ];
  44. const list = document.getElementById('list');
  45. let html = '';
  46. for (let i = 0; i < students.length; i++) {
  47. html += `<li>我的名字是:${students[i].username},${students[i].sex},${students[i].age}</li>`;
  48. }
  49. // console.log(html);
  50. list.innerHTML = html;
  51. </script>
  52. </body>
  53. </html>

箭头函数

箭头函数是什么

认识箭头函数

  1. const add = (x, y) => {
  2. return x + y;
  3. };
  4. console.log(add(1, 1));

箭头函数的结构

// const/let 函数名 = 参数 => 函数体

如何将一般函数改写成箭头函数

  1. // 声明形式
  2. // function add() {}
  3. // 声明形式->函数表达式形式
  4. // const add = function () {};
  5. // 函数表达式形式->箭头函数
  6. // const add = () => { };

箭头函数的注意事项

单个参数

  1. // 单个参数可以省略圆括号
  2. const add = x => {
  3. return x + 1;
  4. };
  5. console.log(add(1));
  6. // 无参数或多个参数不能省略圆括号
  7. // const add = () => {
  8. // return 1 + 1;
  9. // };
  10. // const add = (x, y) => {
  11. // return x + y;
  12. // };
  13. // console.log(add(1, 1));

单行函数体

  1. // 单行函数体可以同时省略 {} 和 return
  2. const add = (x, y) => {
  3. return x + y;
  4. };
  5. //const add = (x, y) => x + y;
  6. console.log(add(1, 1));
  7. // 多行函数体不能再化简了
  8. // const add = (x, y) => {
  9. // const sum = x + y;
  10. // return sum;
  11. // };

单行对象

  1. // const add = (x, y) => {
  2. // return {
  3. // value: x + y
  4. // };
  5. // };
  6. const add = (x, y) => ({
  7. value: x + y
  8. });
  9. // 如果箭头函数返回单行对象,可以在 {} 外面加上 (),让浏览器不再认为那是函数体的花括号
  10. // const add = (x, y) => [x, y];
  11. console.log(add(1, 1));

this指向

全局作用域中的this指向

  1. console.log(this); // window

一般函数(非箭头函数)中的this指向

  1. // 'use strict';
  2. function add() {
  3. console.log(this);
  4. }
  5. // 严格模式就指向 undefined
  6. // add(); // undefined->window(非严格模式下)
  7. // window.add();
  8. // const calc = {
  9. // add: add
  10. // };
  11. // // calc.add(); // calc
  12. // const adder = calc.add;
  13. // adder(); // undefined->window(非严格模式下)
  14. // document.onclick = function () {
  15. // console.log(this);
  16. // };
  17. // document.onclick();
  18. function Person(username) {
  19. this.username = username;
  20. console.log(this);
  21. }
  22. const p = new Person('Alex');
  23. // 只有在函数调用的时候 this 指向才确定,不调用的时候,不知道指向谁
  24. // this 指向和函数在哪儿调用没关系,只和谁在调用有关
  25. // 没有具体调用对象的话,this 指向 undefined,在非严格模式下,转向 window

箭头函数中的this指向

  1. // 1.箭头函数中的 this 指向
  2. // 箭头函数没有自己的 this
  3. // const calc = {
  4. // add: () => {
  5. // console.log(this);
  6. // }
  7. // };
  8. // calc.add(); // window
  9. // 2.练习
  10. 'use strict';
  11. const calc = {
  12. add: function () {
  13. // this
  14. const adder = () => {
  15. console.log(this);
  16. };
  17. adder();
  18. }
  19. };
  20. // calc.add(); // calc
  21. const addFn = calc.add;
  22. addFn(); // undefined->window

不适用箭头函数的场景

作为构造函数

  1. // 箭头函数没有 this
  2. // const Person = () => {};
  3. // new Person();

需要this指向调用对象的时候

  1. // document.onclick = function () {
  2. // console.log(this);
  3. // };
  4. // document.addEventListener(
  5. // 'click',
  6. // () => {
  7. // console.log(this); //window
  8. // },
  9. // false
  10. // );

需要使用arguments的时候

  1. // 箭头函数中没有 arguments
  2. // function add() {
  3. // console.log(arguments);
  4. // }
  5. // add(1, 2,3,4,5);
  6. // const add = () => console.log(arguments);
  7. // add();
  8. // 剩余参数