静态成员和实例成员

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 构造函数Star
     function Star(name,age){
      //通过this添加的成员 实例成员
      this.name = name;
      this.age = age;
   }
   //new 出来一个实例对象 sl
   const sl = new Star('孙俪',22);
   
  //  通过构造函数添加的是 静态成员
  Star.sing = '不仅演技好,唱歌也好好听哦'

  //实例对象不能直接访问静态成员,可以通过找到构造函数的constructor属性调用静态成员
  //构造函数也不能访问到实力成员
  console.log(Star.age); //undefined
  console.log(sl.constructor.sing); //不仅演技好,唱歌也好好听哦
  </script>
</body>
</html>

 

posted @ 2022-08-05 15:37  简法  阅读(12)  评论(0)    收藏  举报