逆推继承看原型 函数的角色 函数声明和函数表达式的区别 函数中this指向的问题

逆推继承看原型

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
      function F1(age){
          this.age = age;
      }
      function F2(age){
          this.age = age;
      }
      F2.prototype = new F1(10);
      function F4(age){
          this.age = age;
      }
      F4.prototype = new F2(20);

      var f4 = new F4(40);
      console.log(f4.age);
  </script>
</head>
<body>
  
</body>
</html>

函数的角色

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
      // 函数的角色:
      // 函数的声明
      function f1(){
        console.log("我是函数");
      }
      f1();
      // 函数表达式
      var ff = function(){
        console.log("我也是一个函数");
      };
      ff();
  </script>
</head>
<body>
  
</body>
</html>

 

函数声明和函数表达式的区别

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>

      // 函数声明      
      // if(true){
      //     function f1(){
      //         console.log("哈哈,我又变帅了");
      //     }
      // }else{
      //     function f1(){
      //         console.log("小苏好猥琐");
      //     }
      // }
      // f1();

      // 函数表达式

      var ff;
      if(true){
          ff = function(){
              console.log("哈哈,我又变帅了");
          };
      }else{
          ff = function(){
              console.log("小苏好猥琐");
          };
      }
      ff();

      // 函数声明如果放在if-else的语句中,在IE8的浏览器中会出现问题
      // 以后宁愿用函数表达式,都不用函数声明

      var ff = function(){

      };
  </script>
</head>
<body>
  
</body>
</html>

函数中this指向的问题

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
      /**
       * 函数中this的指向
       * 普通函数中的this是谁?----window
       * 对象.方法中的this是谁?----当前的实例对象
       * 定时器方法中的this是谁?----window
       * 构造函数中的this是谁?----实例对象
       * 原型对象方法中的this是谁?----实例对象
       * 
       * 
       */
      
      // 严格模式:
      "use strict";
      function f1(){
        // window
        console.log(this);
      }
      window.f1();

      
      // var obj = {
      //     sayHi:function(){

      //     }
      // };

      // 普通函数
      // function f1(){
      //     console.log(this);
      // }
      // f1();

      // BOM中顶级对象是window,浏览器中所有的东西window的

      // 定时器中的this
      // setInterval(function(){
      //   console.log(this);
      // },1000);

      // function Person(){
      //   // console.log(this);
      //   this.sayHi = function(){
      //     console.log(this);
      //   };
      // }
      // Person.prototype.eat = function(){
      //   console.log(this);
      // };
      // var per = new Person();
      // console.log(per);
      // // per.sayHi();
      // per.eat();

      // 普通函数

      // 构造函数

      // 对象的方法

  </script>
</head>
<body>
  
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值