fengchuanyu 2 өдөр өмнө
parent
commit
040ddbf993

+ 0 - 17
8_ES6/21_构造函数.html

@@ -1,17 +0,0 @@
-<!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>
-        // 构造函数
-        // console.log(Array);
-        function Person() {
-            
-        }
-    </script>
-</body>
-</html>

+ 60 - 0
8_ES6/24_构造函数.html

@@ -0,0 +1,60 @@
+<!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>
+        // 构造函数 比如 Array、Object、Function、String、Number、Boolean、Date 等
+        // console.log(Array);
+        // 也可以自定构造函数
+        function Person(userName,age) {
+            this.userName = userName;
+            this.age = age;
+        }
+        // 为构造函数添加方法
+        Person.prototype.sayName = function(){
+            // console.log("我是"+this.userName+",我今年"+this.age+"岁");
+            console.log(`我是${this.userName},我今年${this.age}岁`);
+            
+        }
+        // 调用构造函数
+        // var p1 = new Person("张三",18);
+        // // console.log(p1.userName,p1.age);
+
+        // // 调用方法
+        // p1.sayName();
+        // var p2 = new Person("李四",20);
+        // p2.sayName();
+
+        // 用继承的方式生成一个新的构造函数
+        function Student(userName,age,school) {
+            // 调用父类的构造函数 继承父类的属性
+            Person.call(this,userName,age);
+            this.school = school;
+        }
+
+        //继承父类的方法
+        Student.prototype = new Person();
+
+        // 为子类添加方法
+        Student.prototype.saySchool = function(){
+            console.log(`我来自${this.school}`);
+            
+        }
+
+        // 调用子类的构造函数
+        var s1 = new Student("王五",22,"清华大学");
+        console.log(s1.userName,s1.age);
+        // 调用方法
+        s1.sayName();
+        s1.saySchool();
+
+
+        // ES中没有类,只有构造函数和原型 模拟类
+        
+    </script>
+</body>
+</html>

+ 53 - 0
8_ES6/25_Class.html

@@ -0,0 +1,53 @@
+<!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>
+        // 类的定义 (ES6)使用class关键字
+        class Person{
+            //constructor 构造函数 用于初始化对象的属性
+            constructor(userName,age){
+                this.userName = userName;
+                this.age = age;
+            }
+            // 为类添加方法
+            sayName(){
+                console.log(`我是${this.userName},今年${this.age}岁`);
+            }
+        }
+
+        // 调用类的构造函数
+        // let p1 = new Person("张三",20);
+        // console.log(p1.userName,p1.age);
+        // // 调用方法
+        // p1.sayName();
+
+
+        // 类的继承 (ES6)使用extends关键字
+        class Student extends Person{
+            constructor(userName,age,school){
+                // 调用父类的构造函数 继承父类的属性
+                super(userName,age);
+                this.school = school;
+            }
+            // 为子类添加方法
+            saySchool(){
+                console.log(`我是${this.userName},我来自${this.school}`);
+            }
+        }
+
+        // 调用子类的构造函数
+        let s1 = new Student("李四",18,"清华大学");
+        console.log(s1.userName,s1.age,s1.school);
+        // 调用方法
+        s1.sayName();
+        s1.saySchool();
+       
+
+    </script>
+</body>
+</html>