[TypeScript] Creating a Class in TypeScript
Typescript classes make traditional object oriented programming easier to read and write. In this lesson we learn about class syntax, what the constructor is and some interesting variable features.
interface Opponent {
health: number;
alias: string;
} class ComicBookCharacter { private team: {
name: string;
members: ComicBookCharacter[]
}; static createAndAssignTeam(teamName: string, members: ComicBookCharacter[]) {
let team = {
name: teamName,
members: members
}; members.forEach((member)=>{
member.team = team;
})
} constructor(public alias: string,
public health: number,
public strength: number,
private secretIdentity: string) { } getTeamName(){
console.log(`${this.alias} is from ${this.team.name}`);
return this.team.name;
} attackFunc(opponent: Opponent, attackWith) {
opponent.health -= attackWith;
console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`);
return opponent.health;
} getSecretIdentity() {
if (this.secretIdentity) {
console.log(`${this.alias} is ${this.secretIdentity}`);
} else {
console.log(`${this.alias} has no secret identity`);
}
}
} const Sparky = new ComicBookCharacter("Sparky", , , "Thunder");
const Rainer = new ComicBookCharacter("Rainer", , , "Water"); ComicBookCharacter.createAndAssignTeam('Eevee', [Sparky,Rainer]);
Sparky.getTeamName();
To review, we've learned about access modifiers and the difference between public
and private
. The constructor is run when the class
instance is initialized, and the shorthand for setting class properties allows us to write less code. We've learned that static
properties can only be referenced from the class, not the instance, and how static
properties have access to an instances
private properties.
[TypeScript] Creating a Class in TypeScript的更多相关文章
- [Typescript] Introduction to Generics in Typescript
If Typescript is the first language in which you've encountered generics, the concept can be quite d ...
- 学习TypeScript,笔记一:TypeScript的简介与数据类型
该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27 16:50:03 一.什么是TypeScript? TypeScript是javasc ...
- [TypeScript] Custom data structures in TypeScript with iterators
We usually think of types as something that can define a single layer of an object: with an interfac ...
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
A literal type is a type that represents exactly one value, e.g. one specific string or number. You ...
- [TypeScript] Overload a Function with TypeScript’s Overload Signatures
Some functions may have different return types depending on the types of the arguments with which th ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- 深入浅出TypeScript(2)- 用TypeScript创建web项目
前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...
- Typescript node starter 1.Express Typescript
启动项目 Express 是一个nodejs框架,用于构建Web后端应用程序.它非常的灵活,你可以用你喜欢的方式去使用他.在这个系列文章里,记录了我使用typescript express去构建一个w ...
随机推荐
- marquee
marquee: 页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需 ...
- js24---工厂模式2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- actionBarTab-actionBarTab自定义 布局没法改变其中字体相对中间的位置
我们经常遇到对actionBarTab 进行操作的情况.现在记录修改它的样式的方法,已经如何自定义tab的显示布局 1.在你的theme主题中添加<item name="android ...
- npm install (让别人下载自己的包)
好几天没更新了,再这里跟大家说声抱歉,今天来点干货. 发布一个包在npm上,可以供世界所有人使用,想一下,以前我们做项目,都是在npm install 别人的包,什么时候才能install我们自己的包 ...
- 1.18 Python基础知识 - Python内置函数
官方地址:https://docs.python.org/3.5/library/functions.html abs(x): 返回数字的绝对值 all(iterable): 如果迭代器的所有元素都为 ...
- 使用PyCharm安装第三方库
使用PyCharm安装第三方库是一种十分简单的做法,接下来我来演示一下在PyCharm上安装第三方库requess的操作流程. 首先,先看一下当第三方库未安装时的提示内容,在pycharm中新建pyt ...
- Hadoop学习小结
还在学校的时候,就知道Hadoop的存在了. 2012年在公司实习的时候,买了<Hadoop权威指南第2版>,大致看了下. 今年,抽空也大致喵了几眼. 最大的感悟就是:光看不做,还是不行. ...
- ThinkPHP5.0---删除数据
删除特定记录 public function delete() { // 获取要删除的对象:关键字为16 $Teacher = Teacher::); // 删除对象 $Teacher->del ...
- 一个Java8模型的batch队列
有点小问题,cpu过高,但是思路不错: http://www.tuicool.com/articles/URz2i2q
- 【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永只是期
官网说明 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发人员须要进行妥善保存. access_token的存储至少要保留512个字符空间.ac ...