js笔记(暑假9)ECMAscript结束

74.try catch
1.try里面发生的错误,不会执行try里面错误之后的代码
例1:
    try{
        console.log('a');
        console.log(b);
        console.log('c');
    }catch(e){

    }
    console.log('d');
//执行结果:a d
例2:
    try{
        console.log('a');
        console.log(b);
        console.log('c');
    }catch(e){
        console.log('e');
    }
    console.log('d');
//try里面的代码报错的话,就执行catch里面的东西,如果不报错,那么catch中的东西不会被执行
//如果一旦报错try就会停止之后的代码,直接跳到catch中去执行,可以执行自己想执行的任何东西
//但是一般我们都会打印catch(e)中e里面的信息,e就是系统传过来的一个错误对象(只有两个信息)里面包含了两种信息,一个是e.message和e.name,这个e随便写,就是个形参,写ab什么的都行。

例3:
    try{
        console.log('a');
        console.log(b);
        console.log('c');
    }catch(e){
        alert(e.name + " : " + e.message);
    }
    console.log('d');
//执行结果: a          ReferenceError : b is not defined           d
//这个catch就是负责捕捉错误的,捕捉错误到程序里面,它就不会抛出到控制台,就不会让程序终止,try catch就是为了容错,并且把错误信息给你

2.错误信息类型:六种
Error.name的六种值对应的信息:
1. EvalError:eval()的使用与定义不一致
2. RangeError:数值越界
3. ReferenceError:非法或不能识别的引用数值 
变量未经声明就是使用,函数也是,基本上都是
例:    console.log(b);    
4. SyntaxError:发生语法解析错误 
使用中文字符 ,或者其他低级错误
例:虽然未执行函数,但是也报错
    function demo(){
        :
    }
5. TypeError:操作数类型错误 
6. URIError:URI处理函数使用不当

75.es5.0严格模式
1.现在的浏览器基于es3.0+es5.0新增的方法 去执行的
2.es3.0和es5.0产生冲突的部分
 如果使用的是es5.0严格模式,那么es3.0和es5.0就使用es5.0否则使用es3.0
3:es5.0严格模式的启动
两种用法:
(1)全局严格模式
(2)局部函数内严格模式(推荐)
//语法规定写在页面逻辑最顶端:"use strict";
//但是也可以写在局部但是也要在局部的最顶端,要是写在全局最顶端,那就是全局都采用es5.0的严格模式:
例1:
    "use strict";
    function test(){
        console.log(arguments.callee);
    }
    test();
//执行报错,因为es5.0严格模式不允许arguments.callee,es3.0才允许
例2:
    function test(){
        console.log(arguments.callee);
    }
    test();
    function demo(){
        "use strict";
    }
//正常执行test函数,因为"use strict"在demo函数中
4.为什么写一行字符串"use strict"来实现es5.0的严格模式?写函数strict(){}不行么?
因为假如写函数strict(){}的话,页面放在老浏览器中执行,如果来浏览器没有更新到es5.0的版本的话,那么可能从开头函数strict(){}那里往后都不能执行,就错了
字符串就不会有这样的问题
5.es5.0严格模式不允许使用:
(1)arguments.callee
(2)func.caller
(3)with
作用:
with可以改变作用域链,with可以让它里面的代码的作用域链的最顶端变with括号里的对象,也就是那个对象充当了里面代码的最直接的AO
然后访问变量的话,就直接上那个对象里面去找,把那个对象当作自己的第一个域
要是对象里面没有的话,在从里到外一层一层找,看哪个作用域链里有这个变量的值
但是with太强大了,当代码层级太多的话,with改变作用域链,导致代码失效率。所以es5.0严格模式不允许用with
例1:
    var obj = {
        name : 'obj',
    }
    var name = "window";
    function test(){
        var name = 'test';
        with(obj){
            console.log(name);
        }
    }
    test();
//执行结果返回obj,先上with包含的对象里面去找name,找到为obj,假如obj里面没有name属性,那么就上test作用域链里找,要是test作用域链里没有的话再一层一层往上找,最后是GO
例2:命名空间的真正用法
    var org ={
        dp1 : {
            jc : {
                name : 'abc',
                age : '21',
            },
            deng : {
                name : 'deng',
                age : '21',
            }
        },
        dp2 : {

        }
    }
    with(org.dp1.jc){
        console.log(name);
    }
    with(org.dp1.deng){
        console.log(name);
    }
//执行结果 abc   deng
例3:with其他应用
/*    document :{
        write : function (){

        }
    }*/
    with(document){
        write('a');
    }
//因为document是个对象,里面有很多函数,避免重复多次写比较麻烦,所以放在with中,直接这么写就可以了。
6.es5.0其他规定:
(1)变量赋值前必须声明
例:
b = 10; //不允许,错的
(2)局部this必须被赋值
例1:
    "use strict"
    function Test(){
        console.log(this);
    }
   test();
//执行结果 undefined
例2:
    "use strict"
    function Test(){
        console.log(this);
    }
    new test();
//执行结果:Test{}         这个Test是代表this这个对象的constructor的名字,后面显示的是对象的标准形式
例3:
    "use strict"
    function Test(){
        console.log(this);
    }
    Test.call(123);
//执行结果  123,因为传什么,this就是什么
例4:
/*    "use strict"*/
    function Test(){
        console.log(this);
    }
    Test.call(123);
//执行结果是Number{123}     es3.0在call传进去原始值后就会将其包装为包装类
例5:this在全局里指向window
console.log(this)
//执行结果 window
(3)拒绝重复属性和参数
例1:在es3.0里重复的参数是不报错的,不管结果,就知道不报错就行
    function test(name , name){
        console.log(name);
    }
    test(1,2);
//不会报错 ,不用知道为什么显示2
例2:在es5.0里重复的参数是报错的
    "use strict"
    function test(name , name){
        console.log(name);
    }
    test(1,2);
例3:es5.0本质上是拒绝重复属性的,但是它不报错
    var obj = {
        name : '123',
        name : '234'
    }
//不报错

eval:能把字符串当作代码来执行,但是es3.0都不能使用eval;
    "use strict";
    var a = 123;
    eval('console.log(a)');
//返回 123

猜你喜欢

转载自blog.csdn.net/LFY836126/article/details/81908105