es5、es6、typescript中的类和对象

一.es5

//1.把属性和方法都放在构造函数中(不建议使用,每次创建实例都会创建公共的方法)
function Person(name){
    this.name = name;
    this.getName = function(){
        return this.name;
    }
    this.setName = function(name){
        this.name = name;
    }
    this.action = function(){
        console.log('我是'+this.name)
    }
}
Person.info = function(){
    //静态方法,实例化对象不能调用,只能通过 Person.info()来调用
    console.log('info')
}
Person.age = '20';//静态属性
let teacher = new Person('张三');
console.log(teacher)
console.log(teacher.getName())//张三
teacher.action()//我是张三
teacher.setName('李四');
console.log(teacher)

//2.公共的属性和方法放在原型上
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function (name) {
    return this.name;
}
Person.prototype.setName = function (name) {
    this.name = name;
}
Person.prototype.action = function (name) {
    console.log('我是' + this.name)
}
Person.info = function(){
    //静态方法,实例化对象不能调用,只能通过 Person.info()来调用
    console.log('info')
}
Person.age = '20';//静态属性
let teacher = new Person('张三');
console.log(teacher)
console.log(teacher.getName())//张三
teacher.action()//我是张三
teacher.setName('李四');
console.log(teacher)
//3.继承
//3.1 对象冒充继承,只能继承构造函数中的属性和方法,不能继承原型链上的属性和方法
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function (name) {
    return this.name;
}
Person.prototype.setName = function (name) {
    this.name = name;
}
Person.prototype.action = function (name) {
    console.log('我是' + this.name)
}
Person.info = function(){
    //静态方法,实例化对象不能调用,只能通过 Person.info()来调用
    console.log('info')
}
Person.age = '20';//静态属性

function Teacher(name){
    Person.call(this,name)
}
let teacher = new Teacher('王五');
console.log(teacher)
console.log(teacher.getName())// Uncaught TypeError: teacher.getName is not a function
//3.2原型链继承,能继承父类原型链的属性和方法,不能给父类的构造函数传参
function Person() {
    this.name = '王五';
}
Person.prototype.getName = function (name) {
    return this.name;
}
Person.prototype.setName = function (name) {
    this.name = name;
}
Person.prototype.action = function (name) {
    console.log('我是' + this.name)
}
Person.info = function(){
    //静态方法,实例化对象不能调用,只能通过 Person.info()来调用
    console.log('info')
}
Person.age = '20';//静态属性,只能通过 Person.age来使用

function Teacher(){
}
Teacher.prototype = new Person();
let teacher = new Teacher();
console.log(teacher)
console.log(teacher.getName())
//3.3 组合继承:原型链继承+对象冒充继承
function Parent(name) {
    this.name = name; // 实例基本属性 (该属性,强调私有,不共享)
    this.arr = [1]; // (该属性,强调私有)
}
Parent.prototype.say = function() { // --- 将需要复用、共享的方法定义在父类原型上 
    console.log('hello')
}
function Child(name,like) {
    Parent.call(this,name,like) // 核心  
    this.like = like;
}
Child.prototype = Object.create(Parent.prototype) // 核心  通过创建中间对象,子类原型和父类原型,就会隔离开。不是同一个啦,有效避免了方式4的缺点。

<!--这里是修复构造函数指向的代码-->
Child.prototype.constructor = Child

let boy1 = new Child('小红','apple')
let boy2 = new Child('小明','orange')
let p1 = new Parent('小爸爸')


注意:这种方法也要修复构造函数的
修复代码:Child.prototype.constructor = Child
修复之后:console.log(boy1.constructor); // Child
          console.log(p1.constructor);// Parent  完美

二.es6

//1。创建类和实例化对象,还不支持私有方法和属性
class Person{
  static age = '20';//静态属性
  constructor(name) {
    this.name = name;
  }
  getName(){
    return this.name;
  }
  static action(){
    //静态属性
    console.log(`我是${this.name}`)//此处的name 是Person类的name属性,不是实例的属性
  }
}
let person = new Person('张三');
console.log(person);
console.log(person.getName());//张三
console.log(Person.age);//20
Person.action();//我是Person
//2。继承
class Teacher extends Person{
  constructor(name,age){
    super(name)//super关键字调用了父类的构造函数
    this.age = age;
  }
  getAge(){
    return this.age;
  }
}
let teacher = new Teacher('李四',23);
console.log(teacher);
console.log(teacher.getName());
console.log(teacher.getAge());

三.typescript

//1.类的创建及实例化
class Person{
    name:string;//实例化对象属性,默认是public修饰,必须声明才能在构造函数中赋值
    static name1:string = '李四';//静态属性,只能通过Person.name来访问
    protected name2:string = '王五';//受保护的属性,只能在本类和子类中使用,不能在实例化对象上访问
    private name3:string = '赵六';//私有属性,只能在本类中使用,不能在实例化对象上访问
    constructor(na:string){
        this.name = na;
    }
    getName(){
        //实例化对象方法
        return this.name;
    }
    static getName1(){
        //静态方法,只能通过Person.getName1()来访问
        return this.name1;
    }
    protected getName2(){
        //受保护方法,只能在本类及子类中通过this.getName1()来访问
        return this.name2;
    }
    private getName3(){
        //静态方法,只能在本类中通过Person.getName1()来访问
        return this.name3;
    }
}
let person = new Person('张三');
console.log(person);
console.log(person.getName());
//2.继承
class Teacher extends Person{
    public age:number;
    constructor(na:string,ag:number){
        super(na)//继承了父类的属性和方法
        this.age = ag;
    }
    getAge(){
        return this.age;
    }
}
let teacher = new Teacher('呼保义',20);
console.log(teacher)
console.log(teacher.getName())
console.log(teacher.getAge())
//3.封装:通过公有方法来访问私有属性,get和set实例化对象
class Woker{
    private name:any;
    constructor(na:any){
        this.name = na;
    }
    getName(){
        return this.name
    }
    setName(name:any){
        this.name = name
    }
}
console.log('--------------')
let worker = new Woker('玉麒麟');
console.log(worker.getName())
worker.setName('霹雳火')
console.log(worker)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript 是一种由 Microsoft 开发和维护的开源编程语言,它是 JavaScript 的超集,为 JavaScript 提供了静态类型检查、类和接口等面向对象编程特性,同时也支持最新的 ECMAScript 标准。TypeScript 可以编译成纯 JavaScript 代码,可以运行在任何支持 JavaScript 的平台上。 TypeScript 的主要特点包括: 1. 类型系统:TypeScript 支持静态类型检查,可以提前发现代码的类型错误,减少运行时错误。 2. 类和接口:TypeScript 支持类和接口,可以使代码更加模块化、可读性更高。 3. 泛型:TypeScript 支持泛型,可以提高代码的复用性和灵活性。 4. ES6/7 支持:TypeScript 支持最新的 ECMAScript 标准,并且可以编译成 ES5ES6 代码。 5. 工具支持:TypeScript 支持调试、智能感知、重构等多种工具,可以提高开发效率。 在应用实践TypeScript 已经被广泛应用于 Web 开发、Node.js 开发、桌面应用开发等领域。下面是一些使用 TypeScript 的优秀项目: 1. Angular:Angular 是一个流行的前端框架,它使用 TypeScript 编写。 2. Nest.js:Nest.js 是一个基于 Node.js 的后端框架,它使用 TypeScript 编写。 3. VS Code:VS Code 是一款流行的代码编辑器,它是用 TypeScript 编写的。 4. TypeScript-Node-Starter:TypeScript-Node-Starter 是一个用于快速搭建 TypeScript 后端项目的脚手架。 总的来说,TypeScript 在现代 Web 开发具有重要的地位,它可以提高代码的可读性、可维护性和可扩展性,是一个值得学习和使用的技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值