interface FullName {
firstName: string;
secondName?: string
}
function printName(name: FullName):void {
console.log(name.firstName + '---' + name.secondName)
}
function printInfo(info: FullName):void {
console.log(info.firstName + '---' + info.secondName)
}
interface Config {
type: string;
url: string;
data?: string;
dataType: string;
}
function ajax(config: Config) {
var xhr = new XMLHttpRequest();
xhr.open(config.type, config.url, true)
xhr.send()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
if (config.dataType === 'json') {
} else {
}
}
}
}
ajax({
type: 'get',
data: 'name=zhangsan',
url: 'http://a.itying.com/api/productlist',
dataType: 'json'
})
interface enrypt {
(key:string, value:string):string
}
var md5:enrypt = function(key:string, value: string):string {
return key+value
}
var sha1:enrypt = function(key:string, value: string):string {
return key+value
}
interface UserArr {
[index:number]: string
}
var arr3:UserArr = ['1','2']
console.log(arr3)
interface UserObj {
[index:string]: string
}
var obj:UserObj = {name: '20'}
interface Animal{
name: string;
eat(str:string):void
}
class Dog implements Animal {
name: string;
constructor(name: string) {
this.name = name
}
eat() {
console.log(this.name + '吃肉')
}
}
new Dog('小黑').eat()
class Cat implements Animal {
name: string;
constructor(name: string) {
this.name = name
}
eat(food: string) {
console.log(this.name + '吃' + food)
}
}
new Cat('小猫').eat('鱼')
interface Animal1{
eat():void
}
interface Person extends Animal {
work():void
}
class Programmer {
name: string
constructor(name: string) {
this.name = name
}
coding() {
console.log(this.name + '写代码')
}
}
class Web extends Programmer implements Person {
constructor(name: string) {
super(name)
}
eat() {
console.log(this.name + '在吃')
}
work() {
console.log(this.name + '在工作')
}
}
var w = new Web('王五')
w.eat()
w.work()
w.coding()