zsydgithub 1 年之前
父节点
当前提交
3bdd2b347a
共有 2 个文件被更改,包括 93 次插入0 次删除
  1. 48 0
      es6/11_类.html
  2. 45 0
      es6/12_es6类.html

+ 48 - 0
es6/11_类.html

@@ -0,0 +1,48 @@
+<!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>
+    function Person(name,age){
+      this.name = name
+      this.age = age
+    }
+    /* 
+      所有的构造函数下面都有一个prototype 属性  
+      这个属性指向它的原型对象
+
+      原型对象的特点: 声明在原型对象下面的属性和方法   都被实例化对象所共享
+      属性写在   构造函数里
+      方法写在   原型对象下
+    */
+    Person.prototype.eat = function(){
+      console.log(this.name + 'eat')
+    }
+    var p1 = new Person('zs',30)
+    p1.eat()
+    console.log(p1)
+
+    /* 
+      继承父类的属性 在子类的构造函数里面 通过调用父类的.call 去继承
+      子类的继承对象  =  new 父类 继承方法
+    */
+
+    function Coder(name,age){
+      Person.call(this,name,age)
+    }
+    /* 继承父类的方法 */
+    Coder.prototype = new Person()
+    Coder.prototype.constructor = Coder
+
+    console.log(Coder.prototype.constructor)
+    var c1 = new Coder('lisi',20)
+    console.log(c1)
+    c1.eat()
+  </script>
+</body>
+</html>

+ 45 - 0
es6/12_es6类.html

@@ -0,0 +1,45 @@
+<!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>
+    class Person{
+      constructor(name){
+        this.name = name
+      }
+      eat(){
+        console.log(this,'吃')
+      }
+      //类方法 调用
+      static say(){
+        console.log('shuo')
+      }
+    }
+
+    /* 继承 */
+    class Coder extends Person {
+      constructor(name,xx){
+        super(name)
+        this.xx = xx
+      }
+    }
+
+
+
+    var p1 = new Person('zs')
+    console.log(p1)
+    p1.eat()
+    Person.say()
+
+
+    var c1 = new Coder('lisi','xxxx')
+    console.log(c1)
+    Coder.say()
+  </script>
+</body>
+</html>