e 1 year ago
parent
commit
45eb0d5a27
1 changed files with 40 additions and 0 deletions
  1. 40 0
      JS高级/12.类.html

+ 40 - 0
JS高级/12.类.html

@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>Document</title>
+  </head>
+  <body>
+    <script>
+      function Person(name, age) {
+        this.name = name;
+        this.age = age;
+      }
+      /**
+       * 构造函数中 写属性
+       * 原型中 写方法
+       */
+      Person.prototype.eat = function () {
+        console.log("今天吃了好多");
+      };
+      var p1 = new Person("Lucy", 18);
+      p1.eat();
+      //constructor 构造器
+      /**
+       * 继承父类的方法(继承方法)
+       * 1.子类构造函数里: 通过调用父类的.call继承
+       * 2.子类的继承对象 = new 父类
+      */
+      console.log(Person.prototype.constructor)
+      function Fn1(name,age) {
+        Person.call(this,name,age);
+      }
+      Fn1.prototype = new Person()
+      Fn1.prototype.constructor = Fn1;
+      var p2 = new Fn1("小明",90);
+      console.log(p2);
+      p2.eat();
+    </script>
+  </body>
+</html>