zsydgithub 1 an în urmă
părinte
comite
72cd846642

+ 54 - 0
Ts基础/1_基本数据类型.ts

@@ -96,4 +96,58 @@
 
   //声明一个void变量没什么用 只能赋值 null undefined
   let aaa: void = undefined
+
+
+
+  //object类型
+  //定义一个object类型 参数是object类型 返回值也是object类型
+  function getObj(obj:object): object{
+    console.log(obj)
+    return {
+      name: 'xiaoming',
+      age: 30
+    }
+  }
+  console.log(getObj({name:'zs',age:18}))
+
+  //联合类型  表示取值可以为多种类型
+  //需求1:定义一个函数得到一个数组或者字符串
+  // function toString(x: number | string): string{
+  //   return x.toString()
+  // } 
+  // console.log(toString('123'))
+
+  //需求2: 定义一个函数得到一个数组或者字符串的长度
+  function toString(x: number | string): number{
+    // return x.toString().length
+    // if(x.length){
+    //   return x.length
+    // } else {
+    //   return x.toString().length
+    // }
+
+    if((<string>x).length){
+      return (x as string).length
+    } else {
+      return x.toString().length
+    }
+  }
+  console.log(toString(123))
+
+  //类型断言  告诉编译器 我知道我自己要什么类型 
+  /* 
+    方式1: <类型>值
+    方式2: 值 as 类型
+  */
+
+  // 类型推断 TS会在没有明确指定类型的时候推测出一个类型
+  // let c1 = 100
+  // c1 = 'xiaoming'
+
+  let c1 
+  c1 = 100
+  console.log(c1)
+  c1 = 'xiaoming'
+  console.log(c1)
+
 })()

+ 30 - 0
Ts基础/2_接口.ts

@@ -0,0 +1,30 @@
+//ts的核心原则之一 是对值所具有的结构进行类型检查
+//接口: 是一种类型,是一种规范
+(()=>{
+  /* 
+    需求: 创建一个人的对象 需要对人的属性进行一定的约束
+    id 是number类型  只读
+    name 是string类型  必须有
+    age 是number类型  必须有
+    school 是string类型  可选项
+  */
+  //定义一个接口
+  interface IPerson{
+    readonly id: number
+    name: string
+    age: number
+    school?: string
+  }
+  const person1: IPerson = {
+    id: 1,
+    name: 'xiaoming',
+    age: 30
+  }
+  // person1.id = 40
+  console.log(person1)
+
+  /* 
+    可选属性 ?
+    只读属性 readonly
+  */
+})()

+ 45 - 0
Ts基础/js/1_基本数据类型.js

@@ -80,4 +80,49 @@
     console.log(showMsg());
     //声明一个void变量没什么用 只能赋值 null undefined
     var aaa = undefined;
+    //object类型
+    //定义一个object类型 参数是object类型 返回值也是object类型
+    function getObj(obj) {
+        console.log(obj);
+        return {
+            name: 'xiaoming',
+            age: 30
+        };
+    }
+    console.log(getObj({ name: 'zs', age: 18 }));
+    //联合类型  表示取值可以为多种类型
+    //需求1:定义一个函数得到一个数组或者字符串
+    // function toString(x: number | string): string{
+    //   return x.toString()
+    // } 
+    // console.log(toString('123'))
+    //需求2: 定义一个函数得到一个数组或者字符串的长度
+    function toString(x) {
+        // return x.toString().length
+        // if(x.length){
+        //   return x.length
+        // } else {
+        //   return x.toString().length
+        // }
+        if (x.length) {
+            return x.length;
+        }
+        else {
+            return x.toString().length;
+        }
+    }
+    console.log(toString(123));
+    //类型断言  告诉编译器 我知道我自己要什么类型 
+    /*
+      方式1: <类型>值
+      方式2: 值 as 类型
+    */
+    // 类型推断 TS会在没有明确指定类型的时候推测出一个类型
+    // let c1 = 100
+    // c1 = 'xiaoming'
+    var c1;
+    c1 = 100;
+    console.log(c1);
+    c1 = 'xiaoming';
+    console.log(c1);
 })();

+ 15 - 0
Ts基础/js/2_接口.js

@@ -0,0 +1,15 @@
+//ts的核心原则之一 是对值所具有的结构进行类型检查
+//接口: 是一种类型,是一种规范
+(function () {
+    var person1 = {
+        id: 1,
+        name: 'xiaoming',
+        age: 30
+    };
+    // person1.id = 40
+    console.log(person1);
+    /*
+      可选属性 ?
+      只读属性 readonly
+    */
+})();

+ 2 - 1
Ts基础/页面.html

@@ -7,6 +7,7 @@
   <title>Document</title>
 </head>
 <body>
-  <script src="./js/1_基本数据类型.js"></script>
+  <!-- <script src="./js/1_基本数据类型.js"></script> -->
+  <script src="./js/2_接口.js"></script>
 </body>
 </html>