//1.
function
Person1(name,age){
this.name='zhangsan'
this.age=20
this.run
= function(){
alert('yundong')
}
}
Person.prototype.sex = '男'
Person.prototype.work = function(){
alert('work')
}
var p = new
Person1('zhangsan')
//2.
class
Person3{
name:string //属性,前面省略了public关键词
constructor(name:string){ //构造函数 实例化类的时候触发的函数
this.name = name
}
getName():string{
return this.name
}
setName(name:string):void{
this.name=name
}
}
var p1 = new
Person3('zhangsan')
// ts实现继承
class
Person4{
name:string
constructor(name:string){
this.name=name
}
run():string{
return `$(this.name)在运动`
}
}
// var
p2=new Person4('wangwu')
//
alert(p2.run())
class Web4
extends Person4{
constructor(name:string){
super(name)
}
}
var w = new
Web4()
alert(w.run())
// 3.类里面的修饰符,ts三种:public(公类、子类、类外面) protected(类外面不能访问) private(子类、类外面不能访问)
// 4.静态属性 静态方法
class
Person{
name:string
constructor(name:string){
this.name = name
}
run(){
alert('这是实例方法')
}
static
print(){
alert('这是静态方法')
// 静态方法没办法直接调用类里面的属性
}
}
var p = new
Person('zhangsan')
p.run()
Person.print()