一、严格模式

js的默认执行模式是松散模式,为了规范js这门语言,所以引入了严格模式;目的是是为了消除js的一些怪异行为;

  • 使用严格模式:在js脚本中第一行增加 字符串 “use strict”

严格模式下和普通模式下的常见区别

  1. 在严格模式下形参 和 arguments 没有映射关系

在松散模式中,形参和arguments有映射关系,如果再函数中修改形参,那么arguments中的对应的值也会跟着修改

  1. function sum(a, b) {
  2. b = 100; // 普通松散模式中
  3. console.log(arguments); // 1 100
  4. }
  5. sum(1, 3);

// 严格模式中,形参和arguments不存在映射关系

'use strict';
function sum2(a, b) {
    b = 100;
    console.log(arguments); // 1 2
}
sum2(1, 2);

项目中使用webpack工具打包编译时,会在js脚本头部指定严格模式

  1. 严格模式下的this问题
  • 非严格模式下的call:
function f0() {
    console.log(this);
}
var obj = {
    id: 1
};
f0.call(obj); // this -> obj
f0.call(); // this -> window
f0.call(undefined); // -> window
f0.call(null); // window
  • 严格模式下,call方法不指定this时函数中的this就是undefined
  • 严格模式下call指定谁,谁就是this
'use strict';
function f1() {
    console.log(this);
}
f.call(); // this -> undefined
f1.call(undefined); // this -> undefined
f1.call(null); // this -> null
  1. 严格模式下,不能直接给未声明的变量赋值
'use strict';
aa = 123; // 非严格模式下给window增加属性

aa = 123; // 严格模式下报错: Uncaught Reference:aa is not defined

二、手动抛出错误

  • throw 关键字 用于手动抛出错误
throw 'hello world';
console.log(123); // 不会输出,因为throw关键字抛出的是错误,js一旦抛出错误后就会停止后面的代码执行;
  1. 常见的错误类型
  • 2.1. ReferenceError 引用错误 引用了不存在的变量
throw new ReferenceError('某个变量未找到');
  • 2.2 TypeError 类型错误 错误使用类型
throw new TypeError('你搞错类型了');
  • 2.3 SyntaxError 语法错误
throw new SyntaxError('你的语法错误');
  • 2.4 Error 普通错误
throw new Error('这是一个普通错误');

三、解构赋值

利用数据结构的对应关系快速对数组和对象进行取值;

1. 数组解构赋值:

等号左边的变量的位置和等号右边的数组中值位置相同时,变量可以取得数组中该位置的值;

  • 1.1 顺序解构:
let ary = [10, 50, 100, 200];
  • 没有解构赋值时
let a = ary[0];

let b = ary[1];

let c = ary[2];

console.log(a, b, c);
  • 使用解构赋值
let [a, b, c] = ary; // 等号左边是变量等号右边是数组,要求变量在等号边的位置和想取出的值的索引位置相同
console.log(a, b, c);
  • 1.2 取数组中的某一个:
let [,, d] = ary; // 如果只想得到第三个,前面两个空着,但是需要写两个 ,
console.log(d);
  • 1.3 取出第一项和第二项,剩余的统一放到一个数组中;
let [e, f, ...arr] = ary;
console.log(e); // 10
console.log(f); // 50
console.log(arr); // [100, 200]
  • 1.4 进一步解构:
let ary2 = [10, ['a', 'b']];
let [g, [h, i]] = ary2;
console.log(g); // 10
console.log(h); // 'a'
console.log(i); // 'b'
  • 1.5 数组解构默认值:只有解构出来的值是undefined时默认值才会生效
let ary3 = [1, 3];

let [j = 2, k = 3, l = 100] = ary3; // j的默认值是2,k的默认值是3,l的默认值是100
console.log(j); // 2
console.log(k); // 3
console.log(l); // 100

// 把a当做b的默认值
let [a, b = a] = [1];
console.log(a); // 1
console.log(b); // 1

2. 对象的结构赋值

通常情况下获取一个对象的属性值是通过对象.属性名 或者 对象[‘属性名’]

而对象的解构赋值就是通过变量和对象的属性对应时,就可以取得对象的改属性名的值

let obj = {
    name: '珠峰',
    age: 10
}
  • 2.1解构赋值:
let { name, age } = obj;
console.log(name);
console.log(age);
  • 2.2 如果只想要对象的某一个属性值:
let obj2 = {
    name: '珠峰',
    age: 10,
    courses: ['js前端', '珠峰架构', 'UI设计师']
};
let {courses} = obj2;
  • 2.3 进一步解构:
let obj3 = {
    name: 'zhufeng',
    teacher: {
        js: ['马宾', '牛晓鑫', '....'],
        architect: ['']
    }
};
let { teacher: { js } } = obj3;
console.log(js); // 获取obj3.teacher.js 的值
  • 2.4 重命名:为了避免重复声明
let obj4 = {
    title: '高级前端工程师'
};
let title = 100;
// let { title } = obj4; // 如果直接这样解构,相当于重复声明了title变量
let { title: title2 } = obj4; // 从obj4中解构title属性,并且重命名为title2

console.log(title2);
  • 2.5 解构赋值的默认值:只有解构出来的值是undefined时默认值是才会生效
let obj5 = {
    name: '马宾',
    age: 18,
    job: 'FE',
    // address: undefined
};
let { address = '河北' } = obj5; // 设置address解构赋值的默认值是 '河北';
console.log(address);

// 3. 字符串解构赋值
let [x, y, z] = 'hello';
console.log(x, y, z);

4. 函数参数的解构赋值

function fe({ name: nike, id, num = '18332567506'}) {
    // 把fe执行时传递过来的实参进行解构
    console.log(nike, id, num);
}
fe({
    name: 'zhangsna',
    id: 10
});

5. 对象的简洁语法

let config = '110.156.23.24';
let pwd = '4323445';


let obj6 = {
    config: config,
    pwd: pwd
};
// 当你的属性名和变量名一样时,就可以简写为一个变量名

let obj7 = {
    config,
    pwd,
};

练习:

let response = {
    code: 0,
    data: {
        list: [
            {
                name: 'a',
                age: 1
            },
            {
                name: 'b',
                age: 2
            }
        ],
        page: 1,
        total: 100
    }
};
// 使用解构赋值取出response.list, response.page, response.total
let { data: { list, page, total } } = response;
console.log(list);
console.log(page);
console.log(total);