Typescript 学习 - 类
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);
}
静态属性 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 学习 - 类的更多相关文章
- Typescript 学习笔记五:类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript学习笔记(四)class 类
typescript的类,与c#,java等语言的类类似.也是包含了一大部分的es6的实现.我会用最通俗的语言讲一下对coding有用的地方. class Greeter { greeting: st ...
- TypeScript学习笔记(四) - 类和接口
本篇将介绍TypeScript里的类和接口. 与其他强类型语言类似,TypeScript遵循ECMAScript 2015标准,支持class类型,同时也增加支持interface类型. 一.类(cl ...
- TypeScript学习指南--目录索引
关于TypeScript: TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程. TypeS ...
- TypeScript 学习一 参数,函数,析构表达式
1,TypeScript是由谷歌开发的,并且新出的Angular2框架就是谷歌公司由TypeScript语言编写的,所以现在TypeScript是有微软和谷歌一起支持的: 2,TypeScript在j ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记六:接口
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记二:数据类型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
随机推荐
- 排序算法Java代码实现(四)—— 归并排序
本篇内容: 归并排序 归并排序 算法思想: 将两个或两个以上的有序表合并成一个新的有序表, 即把待排序序列分成若干个子序列,每个子序列是有序的,然后在把有序子序列合并为整体有序序列. 此算法分为两步: ...
- 一次U9身份验证http数据对接
一般情况下传输和回传HTTP协议就搞定了,但这次不同,有身份验证,网上的资料相对较少,怎么办呢?.NET没有不代表JAVA没有,网上搜JAVA身份验证HTTP协议, 果然是有的,跟着代码改成相应的.N ...
- axios安装及使用
使用npm安装 $ npm install axios 使用 bower安装 $ bower install axios 使用 cdn: <script src="https://un ...
- A - A Compatible Pair-biaobiao88
A - A Compatible Pair Nian is a monster which lives deep in the oceans. Once a year, it shows up on ...
- VS Code如何在浏览器中打开Html文件?
1.首先打开扩展 “ 文件 → 首选项 → 按键映射扩展” 快捷键:[ Ctrl+K Ctrl+M ] 2.在出现的窗口输入“open in browser”,安装 3.打开Html文件 Alt+B: ...
- mangodb之save与insert区别
save:未指定 _id 参数 插入成功,自动生成_id指定 _id 但 _id 对应的记录不存在 插入成功,_id不变指定 _id 但 _id 对应的记录存在 根据_id,更新记录 insert: ...
- vue-quill-editor富文本编辑器 中文翻译组件,编辑与展示
vue项目中用到了富文本编辑器,网上找了一些,觉得vue-quill-editor最好用, ui简洁,功能也好配,够用了,文档不好读,有些小细节需要自己注意,我懒得分析,就封装成了组件 大家用的时候直 ...
- 常用SQL语句分享
前言: 日常工作或学习过程中,我们可能会经常用到某些SQL,建议大家多多整理记录下这些常用的SQL,这样后续用到会方便很多.笔者在工作及学习过程中也整理了下个人常用的SQL,现在分享给你!可能有些S ...
- Model 中的Meta类选项
通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...
- Python的WSGI(Web Server Gateway Interface)服务器
Python的WSGI(Web Server Gateway Interface)服务器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.