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

Typescript 学习 - 类的更多相关文章

  1. Typescript 学习笔记五:类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  2. Typescript 学习笔记四:回忆ES5 中的类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  3. Typescript学习笔记(四)class 类

    typescript的类,与c#,java等语言的类类似.也是包含了一大部分的es6的实现.我会用最通俗的语言讲一下对coding有用的地方. class Greeter { greeting: st ...

  4. TypeScript学习笔记(四) - 类和接口

    本篇将介绍TypeScript里的类和接口. 与其他强类型语言类似,TypeScript遵循ECMAScript 2015标准,支持class类型,同时也增加支持interface类型. 一.类(cl ...

  5. TypeScript学习指南--目录索引

    关于TypeScript: TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程. TypeS ...

  6. TypeScript 学习一 参数,函数,析构表达式

    1,TypeScript是由谷歌开发的,并且新出的Angular2框架就是谷歌公司由TypeScript语言编写的,所以现在TypeScript是有微软和谷歌一起支持的: 2,TypeScript在j ...

  7. Typescript 学习笔记七:泛型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  8. Typescript 学习笔记六:接口

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  9. Typescript 学习笔记二:数据类型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

随机推荐

  1. android 入门开发

    本示例讲解的是基本点有 1.使用SQLite数据库 2.对数据的新增,查询. 3.利用ViewActivity进行数据的呈现 代码是参考了网上各种代码,刚开始写,肯定有一些地方是有问题,我对JAVA代 ...

  2. 聊聊 .net Core webAPi 的Get和POST 相关(1)

    上篇文章,我们试着调用API,成功返回值,今天接下来看看代码是怎么构成的 [Route("api/[controller]")] [ApiController] public cl ...

  3. Winform 后台生成饼状图并保存为图片

    .cs代码如下 string ldt_picPath = System.Windows.Forms.Application.StartupPath + @"Pic\" + Item ...

  4. oracle中如何生成awr【性能调优】报告

    1.进入数据库 sqlplus / as sysdba 2.查看用户 show parameter db_name 3.开始压测后执行 exec DBMS_WORKLOAD_REPOSITORY.CR ...

  5. python基础知识(最基本)

    保留字(关键字)   False None True and as break class continue def elif else except finally for from global ...

  6. javascript高级程序设计学习历程

    第三章 基本概念 3.1 语法 3.1.1 区分大小写 ECMAScript中的一切(变量,函数,操作符)都区分大小写的 3.1.2 标识符 标识符:变量,函数,属性的名字以及函数的参数. 标识符的命 ...

  7. pdf.js实现图片在线预览

    项目需求 前段时间项目中遇到了一个模块,是关于在线预览word文档(PDF文件)的,所以,找了很多插件,例如,pdf.js,pdfobject.js框架,但是pdfobject.js框架对于IE浏览器 ...

  8. Centos7安装pip或pip3

    1.使用Python2安装pip wget wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip- ...

  9. MySQL数据库(一)-- 数据库介绍、MySQL安装、基础SQL语句

    一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 (3)一个程序不太可能仅运行在同一台电脑上 ...

  10. Linux操作系统的打包/归档工具介绍

    Linux操作系统的打包/归档工具介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.