class

class 并不是一种新的数据结构,只是在函数原型基础上的语法糖

class People {
hand: number;
constructor(hand: number) {
this.hand = hand;
}
getHandNum(): void {
console.log(this.hand)
}
}

转为 js

var People = /** @class */ (function () {
function People(hand) {
this.hand = hand;
}
People.prototype.getHandNum = function () {
console.log(this.hand);
};
return People;
}());

extends 派生类

派生类包含了一个构造函数,它 必须调用 super(),它会执行基类的构造函数。 而且,在构造函数里访问 this 的属性之前,我们 一定要调用 super()。 这个是TypeScript强制执行的一条重要规则。

public、private、protected 区别

  • public 是默认值,公有的
  • private 是私有的,继承或者实例化都不能访问
  • protected 是保护类型的,继承可以访问,实例化不行
class Animal {
private hand: string;
constructor(hand) {
this.hand = hand;
}
} let dog = new Animal('abc');
// 会报错 Property 'hand' is private and only accessible within class 'Animal'.
dog.hand;

存取器

自定义原型上方法的 get 和 set ,如果只定义 get 的话,会被 ts 推断为 readonly

let passcode = "secret passcode";

class Employee {
private _fullName: string; get fullName(): string {
return this._fullName;
} set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
} let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
alert(employee.fullName);
}

转为js

var passcode = "secret passcode";
var Employee = /** @class */ (function () {
function Employee() {
}
// 重写函数原型上的 get 和 set Object.defineProperty(Employee.prototype, "fullName", {
get: function () {
return this._fullName;
},
set: function (newName) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
},
enumerable: true, // 属性是否可枚举
configurable: true // 属性是否可配置
});
return Employee;
}());
var employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
alert(employee.fullName);
}

属性描述符 MDN

静态属性 static 关键字

class Grid {
static origin = {x: 0, y: 0};
calculateDistanceFromOrigin(point: {x: number; y: number;}) {
let xDist = (point.x - Grid.origin.x);
let yDist = (point.y - Grid.origin.y);
return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
}
constructor (public scale: number) { }
}

转为 js

var Grid = function(){
this.origin = {x: 0, y: 0};
}
Grid.prototype.calculateDistanceFromOrigin = () => {
let xDist = (point.x - Grid.origin.x);
let yDist = (point.y - Grid.origin.y);
return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
}
Grid.origin = {x: 0, y: 0};

抽象类 abstract

abstract class ParentClass {
// 抽象类中定义的抽象方法,必须在子类中实现
abstract ParentFun(): void;
}

实例的类型

class Greeter {
greeting: string;
constructor(greeting: string) {
this.greeting = greeting
}
onGreet() {
console.log(this.greeting);
}
}
// 实例的类型是 Greeter 类
let greet: Greeter;
greet = new Greeter('hello');
greet.onGreet();

把类当作接口使用

class Point {
x: number;
y: number;
} interface Point3d extends Point {
z: number;
} let point3d: Point3d = { x: 1, y: 2, z: 3 };

类可以创造出类型,所以可以在接口中使用类

什么情况用类或接口表示类型???

类ts转换代码是会转为function...

接口只是在编译的时候做的类型约定,不会转成任何代码

interface Point {
x: number;
y: number;
} interface Point3d extends Point {
z: number;
} let point3d: Point3d = { x: 1, y: 2, z: 3 };

转为 js

var point3d = { x: 1, y: 2, z: 3 };

参考文档

https://www.tslang.cn/docs/handbook/classes.html

https://www.tslang.cn/play/index.html

最新文章

  1. pomelo获取客户端IP
  2. FreeRTOS--删除任务
  3. jquery的siblings()
  4. c# 中几个关于string问题
  5. 【转载】linux环境下tcpdump源代码分析
  6. c++const小结
  7. linux find详解
  8. php 异步
  9. 非服务器的定期校正时间 Anacron
  10. ADALINE模型
  11. Linux积累 命令之cat和wc
  12. ./runInstaller: Permission denied
  13. ASP.NET Core Web App应用第三方Bootstrap模板
  14. Codeforces1023E Down or Right 【贪心】
  15. mycat配置文件备份
  16. form提交循环
  17. C# 获得本机IP、端口等信息地址以及服务器IP信息
  18. 《Exploring in UE4》多线程机制详解[原理分析]
  19. priority_queue的优先级变化(结构体的写法)
  20. mongodb更新数组中的所有匹配项

热门文章

  1. Python——全国瓜子二手车数据分析
  2. Linux系统SSH免密登录
  3. Spring所有注解大揭秘
  4. HTML 统一资源定位器
  5. Android 可单选多选的任意层级树形控件
  6. windows环境下基于nginx搭建rtmp服务器
  7. 【GPU加速系列】PyCUDA(一):上手简单操作
  8. LCD编程_使用调色板
  9. CSP 201812-2 小明放学
  10. Linux 命令 mv