瀏覽代碼

fix:day23

e 1 年之前
父節點
當前提交
030524ae9b
共有 4 個文件被更改,包括 114 次插入0 次删除
  1. 11 0
      day23/html/3.类.html
  2. 11 0
      day23/html/4.class类.html
  3. 39 0
      day23/js/3.类.js
  4. 53 0
      day23/js/4.class类.js

+ 11 - 0
day23/html/3.类.html

@@ -0,0 +1,11 @@
+<!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 src="../js/3.类.js"></script>
+</body>
+</html>

+ 11 - 0
day23/html/4.class类.html

@@ -0,0 +1,11 @@
+<!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 src="../js/4.class类.js"></script> 
+</body>
+</html>

+ 39 - 0
day23/js/3.类.js

@@ -0,0 +1,39 @@
+function Person(name,age) {
+    this.name = name;
+    this.age = age;
+}
+
+/**
+ * 所有构造函数 都有一个prototype属性 这个属性指向它的原型对象
+ * 原型对象的特点:声明在原型对象下的属性和方法 都能被实例化对象所共享
+ * 属性 构造函数下
+ * 方法 原型对象下
+ */
+
+Person.prototype.eat = function() {
+    console.log("eat");
+}
+
+var p1 = new Person();
+p1.eat();
+
+// 构造函数constructor
+console.log(Person.prototype.constructor);
+
+/**
+ * 继承父类的方法 
+ * 在子类构造函数里 通过调用父类的.call去继承
+ * 子类的继承对象 = new 父类 继承方法
+ */
+
+function fn1(name,age) {
+    // this 当前对象
+    Person.call(this,name,age)
+}
+/**继承父类的方法 */
+fn1.prototype= new Person();
+
+fn1.prototype.constructor = fn1;
+var p2 = new fn1("HH",20);
+console.log(p2,'p2');
+p2.eat();

+ 53 - 0
day23/js/4.class类.js

@@ -0,0 +1,53 @@
+/**
+ * ES6提供了class(类)的概念;
+ * 通过class定义关键字,可以是定义类
+ * class 定义类
+ * constructor 定义构造函数初始化
+ * extends 继承父类
+ * super 调用父级构造函数方法
+ * static 定义静态的方法和属性 
+ */
+
+// class定义类
+class Phone{
+    // 构造函数
+    constructor(brand,color,price){
+        this.brand = brand;
+        this.color = color;
+        this.price = price;
+
+    }
+    // 定义方法
+    call(){
+        console.log("请给我打电话!")
+    }
+}
+
+
+// 子类
+class newPhone extends Phone {
+    constructor(brand,color,price,vase,small){
+        super(brand,color,price);
+        this.vase = vase;
+        this.small = small;
+    }
+    photo() {
+        console.log("拍照");
+    }
+    music() {
+        console.log("音乐");
+    }
+    static run() {
+        console.log("程序开始启动");
+    }
+    static end() {
+        console.log("程序结束运行");
+    }
+}
+
+var a1 = new Phone("我的","你的",300);
+var a2 = new newPhone("12","23","34","我哦","掘金");
+console.log(a1,'a1');
+console.log(a2,'a2');
+a2.call();
+newPhone.run();