1. 程式人生 > >js中物件的3種表示方法

js中物件的3種表示方法

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<!--物件表示方法1-->
  <script>
      function Person(){
          var person = {
              name:"zhh1",
              eat:function(){
                  document.write("我愛吃米飯1")
              }
          }
          return person;
      }
      var person = new Person();
      document.write(person.name);
      person.eat();
  </script>

  <!-- 物件表示方法2-->
  <script>
      var man ={
          name:"美男2",
          eat:function(){
              document.write("美男愛吃米飯2")
          }
      }
      document.write(man.name);
      man.eat();
  </script>

<!--物件表示方法3-->
  <script>
      function WoMan(){}
      WoMan.prototype={
          name:"美女3",
          eat:function(){
              document.write("美女愛吃米飯3")
          }
      }
      var woman = new WoMan();
      document.write(woman.name);
      woman.eat();
  </script>


</body>
</html>