JS原型及原型链 严格模式 异常处理

一、原型及原型链
   //给Person构造函数添加原型对象
    Person.prototype = {
        constructor: Person,     // 构造函数的原型对象中的构造函数指向他本身
        name: "",
        eat: function () {
        }
    }
    //Person 构造函数
    function Person() {
        this.color = "";
        this.sleep = function () {
        }
    }
    var p = new Person();         // 对构造函数进行实例化,生成一个对象
    console.log(p);
    console.log(p.__proto__);     //原型对象Person
    console.log(p.__proto__.__proto__);   //原型链 object 的原型对象

在这里插入图片描述

二、严格模式
<script>
   "use strict"   // 在代码前写上这句话,就代表的是严格模式,后面的代码就要写的规范,否则会报错
    var x = 10; 
    m = 20;
</script>
三、异常处理

try: 测试代码块的异常
catch: 处理异常
throw:抛出异常
finally: 无论是否有触发异常,最后该语句都会执行

  1. 如果try语句中有异常,则执行catch语句,如果 try语句中没有异常,就不执行catch语句,直接执行finally语句。
  2. 如果catch中有错误的语句,finally中没有,则报catch中的错误;如果catch中有错误的语句,finally中也有,则报finally中的错误,catch中的不报。先执行完所有的正常语句,最后再报错误信息
  3. e 是错误信息
   //  1. try 语句中没有错误
        try{
                console.log(0);
            }catch (e){
                console.log(1);
                console.log(hello);
            }finally {
                console.log(2);
           }          // 结果是:0  2

       
  //  2. try 语句有错误,catch有错误
        try{
             a();
            }catch (e){
                console.log(1);
                console.log(hello);
            }finally {
                console.log(2);
            }               // 结果是:1  2  Uncaught ReferenceError: hello is not defined

            
  //  3. try 语句中没有错误,catch有错误,finally也有错误
          try{
              console.log(a, b, c);
            }catch (e){
                 console.log(1);
                 console.log(hello);
             }finally {
                 console.log(2);
                 console.log(word);
             }          // 结果是:1  2  Uncaught ReferenceError: word is not defined
             
   //  4. try 语句的嵌套
        try{
               a.b.c();
           }catch (e){
               console.log(1);
              // console.log(hello);
               try{
                   console.log(word);
               }catch (e){
                   console.log(hello);
               }
           }finally {
               console.log(2);
           }          // 结果是:1  2  Uncaught ReferenceError: hello is not defined
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值