typescript的学习之路

"use strict";
// let a: string;
// a = 'alsdjfklasj'
// function all(name: string, age: number): string {
//     return `${name},${age}`
// }
// // alert(all('张三',20))
// // undefined类型
// let und: undefined;
// und = undefined;
/*
    // number类型
    let num:number;
    num = 123;
*/
/*
    // string类型
    let num:string;
    num = '123';
*/
/*
    // null类型
    let num:null;
    num = null;
*/
/*
    // 数组类型
   形式1:
    let arr:string[]=['q','q']
    console.log(arr)
   形式2 泛型
    let arr:Array<number> =[1,1,3,4,56,7]
    console.log(arr);
    元组
    let arr2:[string,number] = ['112',123]
*/
/*
    // 枚举类型
    enum test {
        a=2,
        b,
        c
    }
    console.log(test.a,test.b,test.c); //2,3,4
*/
/*
    // never类型
   ((msg:string):never=>{
    throw new Error(msg);
    })('参数有误')
*/
/*
    // any类型
    console.log(((name:string,age:number):any=>{
        return name;
    })('张三',123));
*/
/*
    // 空值类型 void
    let arr = ():void=>{
    console.log(123456789);
    }
    arr() //123456789


    let arr = ():void=>{
    return 123 //报错
    }
*/
/*
    // 布爾類型 boolean 值為true/false
    let boon:boolean = true
*/
/*
    // 類型斷言
    let str:string = '九歲瘋去吧'
    let num:number = (<string>str).length
*/
// function Person(name:string,age:number) {
//     this.name=name;//屬性
//     this.age=age
//     this.run = ()=>{
//         console.log(`${this.name}在運動`)
//     }
// }
// Person.aeg = function(){
//     alert(111)
// }
// Person.prototype.sex = '男'
// Person.prototype.work=function(){
//     console.log(`${this.name}在工作`)
// }
// var man = new Person('張三',20)
// console.log(man.name,man.age)
// man.run()
// man.work()
// console.log(man.sex)
// Person.aeg()
// abstract class Anmal {
//     name:String
//     constructor(name:string){
//         this.name=name
//     }
//     abstract eat():void
// }
// class cat extends Anmal{
//     constructor(name:string){
//         super(name)
//     }
//  eat():void{
//      console.log('猫吃老鼠')
//  }
// }
// 定义接口
// interface Ajax {
//     url:string,
//     methods:string,
//     data:string,
//     datatype:string
// }
// function ajax(config:Ajax){
//     var xhr = new XMLHttpRequest()
//     xhr.open(config.methods,config.url,true)
//     xhr.send(config.data)
//     xhr.onreadystatechange=function(){
//         if(xhr.status==200&&xhr.readyState==4){
//             if(config.datatype=='jsonp'){
//                 console.log(JSON.parse(xhr.responseText))
//             }else {
//                 console.log(xhr.responseText)
//             }
//         }
//     }
// }
// ajax({
//     url:'http://www.baidu.com',
//     methods:'post',
//     data:'name',
//     datatype:'jsonp'
// })  
/*
// let arr:Array<T> = [1,'asdf']
// 定义数组
let arr:Array<number> = [1,2,3]
// 元祖
let arr2:[number,string,string]=[1,'s','s']
// 定义undefined
let und:undefined = undefined
// 定义null
let nu:null=null
// 定义数字
let num:number=1
// 定义字符串
let str:string = '123'
// 定义boolean
let is:boolean=true
// 定义void
function getname(name:string):void {
    console.log(123)
}
// 定义枚举
 enum index{
    i=1,
    n
}
// console.log(index.i)
// 定义any
function getnum(num:any):any{
    return num
}
// console.log(getnum('jksadfh').length.toString())
// 定义never
var a:never;
a=()=>{
    throw new Error('错误')
}
a()
*/
/*
// typescript中的函数
    // es5中定义函数
// 第一种方式
function fun(){
    console.log(11);
}
// 第二种方式
// 匿名函数
var fun1 = function(){
    return 1
}
// ts中定义函数的方式
function fun3():number {
    return 2
}
var fun4 = function():number {
    return 2
}

// ts中定义函数传参
function fun5(name:string,age:number):void {
    console.log(123)
}
// ts中定义函数的可选参数
function fun6(name:string,age?:number):void {
    console.log(1)
}
fun6('1',1) //正确
fun6('1')   //正确
// 注意 :  可选参数必须要定义在最后
// ts中的默认参数
function fun7(name:string='sss',age:number):void {
    console.log(123)
}
fun7('sss',0)
// es定义函数时的剩余参数
function fun8(...num:number[]):void{
    console.log(...num)
}

fun8(1,2,3,4,5,6,7,8,9)
*/
/*
    es中的类
    
//  function Person(){
//      this.name='张三'
//      this.age=20
//  }
//  let zs =new Person()
//  console.log(zs.name)
// function Person {
//     this.name = '张三';
//     this.age = 20
//     this.say = function(){
//         alert(111)
//     }
// }
// var p = new Person()
// p.say()
*/
/*
    es5 中的继承
*/
/* 借用继承
function Person() {
    this.name = '张三';
    this.age = 20;
    this.say = function () {
        alert(111);
    };
}
Person.prototype.gender = '男';
Person.prototype.work = function () {
    console.log('工作');
};
function People() {
    Person.call(this);
}
var p = new People();
console.log(p.name);
p.say(); // 借用继承可以继承类的属性和方法 ,但无法继承类的原型上的属性和方法
console.log(p.gender);
p.work();

*/
/*     原型继承  ---------原型继承可以继承类的属性和方法,也可以继承类原型上的属性和方法
                    -------------》》》》》》》 问题 无法接受参数


function Person() {
    this.name = '张三';
    this.age = 20;
    this.say = function () {
        console.log(111);
    };
}
Person.prototype.gender = '男';
Person.prototype.work = function () {
    console.log('工作');
};
function People() {
}
People.prototype=new Person()
var p = new People()
console.log(p.name);
console.log(p.gender);
console.log(p.age);
p.say()
p.work() 

*/



/* 原型继承的缺点


function Person(name,age) {
    this.name = name;
    this.age = age;
    this.say = function () {
        console.log(111);
    };
}
Person.prototype.gender = '男';
Person.prototype.work = function () {
    console.log('工作');
};
function People() {
}
People.prototype=new Person()
var p = new People('zzhgna',21)
console.log(p.name);    // undefined
console.log(p.gender);  //  男
console.log(p.age);     // undefined
p.say()                 //111
p.work()                //工作



*/
  

/* 借用继承和原型继承的结合

function Person(name,age) {
    this.name = name;
    this.age = age;
    this.say = function () {
        console.log(111);
    };
}
Person.prototype.gender = '男';
Person.prototype.work = function () {
    console.log('工作');
};
function People(name,age) {
    Person.call(this,name,age)
}
People.prototype=new Person()
var p = new People('zzhgna',21)
console.log(p.name);    // zzhgna
console.log(p.gender);  //  男
console.log(p.age);     // 21
p.say()                 //111
p.work()                //工作



*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值