TypeScript类前奏之ES5中的类

TypeScript类

//es5中的类:众所周知,在es5中是用构造函数来定义一个类
function Person () {
  this.name = 'ag'; // 属性
  this.age = 23;
  // 属性方法
  this.run = function () {
    console.log(this.name + '在跑步')
  }
}
// 静态方法
Person.staticWork = function () {
  console.log('静态方法在工作')
}
Person.prototype.sex = '男' // 原型链上属性
Person.prototype.work = function () { // 原型链上方法
  console.log(this.name + '在工作')
}
var person = new Person()
person.run()
console.log(person.age)
Person.staticWork()
person.work()


//es5中的继承:原型链+对象冒充的组合继承模式
function Student(){
    Person.call(this);
}

var student = new Student();
student.run();//对象冒充可以继承里面的属性和方法
student.work(); //会报错,对象冒充无法继承原型链上的属性和方法

在es5中对象冒充可以继承构造函数里面的属性和方法,对象冒充无法继承原型链上的属性和方法。

原型链实现继承:

原型链继承
function Person () {
  this.name = 'ag'; // 属性
  this.age = 23;
  // 属性方法
  this.run = function () {
    console.log(this.name + '在跑步')
  }
}
// 静态方法
Person.staticWork = function () {
  console.log('静态方法在工作')
}
Person.prototype.sex = '男' // 原型链上属性
Person.prototype.work = function () { // 原型链上方法
  console.log(this.name + '在工作')
}
var person = new Person()
person.run()
console.log(person.age)
Person.staticWork()
person.work()


//原型链继承模式
function Student(){
 }

Student.prototype = new Person(); //原型链继承
var student = new Student();
student.run();
student.work();
 

在es5中的原型链继承,既可以继承构造函数里面的属性和方法,也可以继承原型链上的属性和方法。

//原型链继承
function Person (name,age) {
  this.name = name; // 属性
  this.age = age;
  // 属性方法
  this.run = function () {
    console.log(this.name + '在跑步')
  }
}
// 静态方法
Person.staticWork = function () {
  console.log('静态方法在工作')
}
Person.prototype.sex = '男' // 原型链上属性
Person.prototype.work = function () { // 原型链上方法
  console.log(this.name + '在工作')
} 


//原型链继承模式
function Student(){
 }

Student.prototype = new Person(); //原型链继承
var student = new Student('赵四',23);
student.run(); 
student.work(); 
//undefined在跑步
//undefined在工作

构造函数实现继承的时候,实例化子类的时候没办法给父类传参.

为了解决以上问题,引出:

原型链+构造函数的组合继承模式

很简单就是将以上两者组合起来

//原型链继承
function Person (name,age) {
  this.name = name; // 属性
  this.age = age;
  // 属性方法
  this.run = function () {
    console.log(this.name + '在跑步')
  }
}
// 静态方法
Person.staticWork = function () {
  console.log('静态方法在工作')
}
Person.prototype.sex = '男' // 原型链上属性
Person.prototype.work = function () { // 原型链上方法
  console.log(this.name + '在工作')
} 


//原型链继承模式
function Student(name,age){
  Person.call(this,name,age); //对象冒充,可以继承构造函数里的属性和方法 
 }

Student.prototype = new Person(); //原型链继承
var student = new Student('赵四',23);
student.run(); 
student.work();
//赵四在跑步
//赵四在工作

原型链+对象冒充的另外一种方式

//原型链继承
function Person (name,age) {
  this.name = name; // 属性
  this.age = age;
  // 属性方法
  this.run = function () {
    console.log(this.name + '在跑步')
  }
}
// 静态方法
Person.staticWork = function () {
  console.log('静态方法在工作')
}
Person.prototype.sex = '男' // 原型链上属性
Person.prototype.work = function () { // 原型链上方法
  console.log(this.name + '在工作')
} 


//原型链继承模式
function Student(name,age){
  Person.call(this,name,age); //对象冒充,可以继承构造函数里的属性和方法 
 }

Student.prototype = Person.prototype; //原型链继承
var student = new Student('赵四',23);
student.run(); 
student.work();
//赵四在跑步
//赵四在工作

原理是将

Student.prototype = new Person();

改为:

Student.prototype = Person.prototype;

就是让他直接继承父类的原型链。这样既可以继承父类的属性和方法,也可以继承父类的原型链。

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值