e 2 сар өмнө
parent
commit
c552ca7dd7

+ 1 - 1
14.ts/3.编译选择/src/b.ts

@@ -5,7 +5,7 @@ let a;
 a = '12';
 a = undefined;
 console.log(a,'a')
-function fn1(a:any,b:any):any {
+function fn1(a:any,b:string):any {
     console.log(a+b);
 }
 fn1(1,'76')

+ 23 - 0
14.ts/4.面向对象/dist/1.类.js

@@ -0,0 +1,23 @@
+// class 去声明类
+console.log("你好");
+class Person {
+    constructor() {
+        this.name = "John";
+        this.age = 10;
+    }
+    static say() {
+        console.log("大家好");
+    }
+}
+/***
+ * static 添加后 变成了类方法
+ * name 规避字段
+ * readonly 只读
+ */
+// 实例化对象
+let p = new Person();
+// p.name = '图图';
+// p.age = 12;
+// p.say();
+Person.say();
+console.log(new Person());

+ 18 - 0
14.ts/4.面向对象/dist/2.构造函数和this.js

@@ -0,0 +1,18 @@
+// 构造函数 => 函数
+// function Fn1() {
+// }
+// Fn1();
+// new Fn1();
+// 构造函数:构造器(构造函数)(属性) 原型(方法)
+class Person1 {
+    constructor(name, age) {
+        this.name = name;
+        this.age = age;
+        console.log(this, 'this');
+    }
+    hello() {
+        console.log("你好");
+    }
+}
+let p1 = new Person1('孙悟空', 20);
+p1.hello();

+ 11 - 0
14.ts/4.面向对象/index.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="./dist/2.构造函数和this.js"></script>
+</body>
+</html>

+ 21 - 0
14.ts/4.面向对象/src/1.类.ts

@@ -0,0 +1,21 @@
+// class 去声明类
+console.log("你好")
+class Person {
+  readonly name:string="John";
+    age:number =10;
+  static say() {
+    console.log("大家好")
+   }
+}
+/***
+ * static 添加后 变成了类方法
+ * name 规避字段
+ * readonly 只读
+ */
+// 实例化对象
+let p = new Person();
+// p.name = '图图';
+// p.age = 12;
+// p.say();
+Person.say();
+console.log(new Person())

+ 21 - 0
14.ts/4.面向对象/src/2.构造函数和this.ts

@@ -0,0 +1,21 @@
+// 构造函数 => 函数
+// function Fn1() {
+// }
+// Fn1();
+// new Fn1();
+// 构造函数:构造器(构造函数)(属性) 原型(方法)
+class Person1 {
+    name:string;
+    age:number;
+    constructor(name,age) {
+        this.name = name;
+        this.age = age;
+        console.log(this,'this')
+    }
+    hello() {
+        console.log("你好")
+    }
+}
+
+let p1 = new Person1('孙悟空',20)
+p1.hello();

+ 12 - 0
14.ts/4.面向对象/tsconfig.json

@@ -0,0 +1,12 @@
+{
+    "include": [
+        "./src/**/*"
+    ],
+    "compilerOptions": {
+        "moduleResolution": "Node",
+        "target": "ES2015",
+        "module": "ES2015",
+        "outDir": "./dist",
+        // "strict": true
+    }
+}