假设类中创建的 readonly 类型的属性,该类型的属性只能在声明处或构造器中进行初始化。

class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor (theName: string) {
this.name = theName;
}
}

为了初始化 name 属性,不得不在构造器中声明另一个入参 theName。这显得冗余。

TypeScript 提供了在构造器上同时完成属性的声明和初始化的功能。

以下代码和上面的等效:

class Octopus {
readonly numberOfLegs: number = 8;
constructor(readonly name: string) {
}
}

这种通过在构造器的入参中声明属性的方式叫作 Parameter properties

通过在构造器入参上添加访问限定符(accessibility modifier ),readonly 或两者结合,该参入便会成为类的属性。

一个比较综合的示例:

class Foo {
a: string;
public b: string;
protected c: string;
constructor(d: number, public e: string) {}
} var foo = new Foo(1, "2"); console.log(foo.a); // ✅ a 没有修饰词,和 C++ struct 默认公有表现一样为 public,与 C++ class 默认私有刚好相反

console.log(foo.b); // ✅ b 是公有

console.log(foo.c); //

TypeScript 参数属性的更多相关文章

  1. Windows消息传递函数SendMessage参数属性

    Windows消息传递函数SendMessage参数属性 转载于:http://www.cr173.com/html/5605_1.html Windows是一个消息驱动式系统,SendMessage ...

  2. Angular2入门:TypeScript的类 - 参数属性:定义和初始化类成员

  3. typescript静态属性,静态方法,抽象类,多态

    /* 1.vscode配置自动编译 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js", 2.第二步 任务 ...

  4. CLR via C#(11)-无参属性、有参数属性(索引器)

    一. 无参属性 1. 定义属性 无参属性就是我们最常见的属性方式,在赋值时可以加入一定的逻辑判断.属性的定义其实不复杂,先看个直观的例子: 说明: 属性要定义名称和类型,且类型不能是void. 属性是 ...

  5. Uploadify 3.2 参数属性、事件、方法函数详解

    一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass ” 按钮样式 buttonCursor ‘ ...

  6. (转)Uploadify 3.2 参数属性、事件、方法函数详解

    转自http://blog.sina.com.cn/s/blog_5079086b0101fkmh.html Hallelujah博客 一.属性 属性名称 默认值 说明 auto true 设置为tr ...

  7. odoo之model参数属性1

    1.基础文件及目录结构 在认识odoo ORM框架前,先介绍一下odoo中模块目录结构.   data:存放模块预制数据 i18n:存放国际化文件 models:存放模型等py代码 security: ...

  8. KindEditor-编辑器配置参数属性

    KindEditor-源码分析 通过使用KE.show(config)方法即可将编辑器添加到文档中.下面是一段源码: KE.show = function(args) {     KE.init(ar ...

  9. struts获得参数(属性,对象,模型驱动)

    0. strutsMVC

随机推荐

  1. 【读一本书】《昇腾AI处理器架构与编程》--神经网络基础知识(2)

    1 卷积神经网络:输入层 之前提到多层感知机的参数太多,导致训练耗时长并且对图像处理也不具有优势,因此大神们 就提出了多层神经网络,其中最经典的是卷积神经网络(Convolution Neural N ...

  2. [leetcode] H-Index (Hash Table)

    题目: Given an array of citations (each citation is a non-negative integer) of a researcher, write a f ...

  3. MySQL必知必会(组合Where子句,Not和In操作符)

    SELECT prod_id, prod_price, prod_name FROM products ; SELECT prod_id, prod_price, prod_name FROM pro ...

  4. 【原创】004 | 搭上SpringBoot事务诡异事件分析专车

    前言 如果这是你第二次看到师长,说明你在觊觎我的美色! 点赞+关注再看,养成习惯 没别的意思,就是需要你的窥屏^_^ 本专车系列文章 目前连载到第四篇,本专题是深入讲解Springboot源码,毕竟是 ...

  5. luogu P1031 均分纸牌

    题目很简单,但是可以学一学贪心策略 把纸牌均匀分布,从左往右推掉不用的纸牌 #include <iostream> using namespace std; int main() { in ...

  6. Xcode9 脚本打包报错

    Xcode9 脚本编译报错   xcodebuild -exportArchive fails with error Locating signing assets failed X9的exporto ...

  7. Creating your first iOS Framework

    转自:https://robots.thoughtbot.com/creating-your-first-ios-framework If you’ve ever tried to create yo ...

  8. POJ 3281 Dining(网络流-拆点)

    Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...

  9. Linux-部署-Django

    Linux-部署-Django-项目过程与问题总结 优才网 2017-04-12 18:00   本篇主要用于记录部署 Django 项目所有踩过的坑. 最近学习 Django 框架开发,将项目部署到 ...

  10. 学习了JsonSchema,我自定义了一个校验代码

    JsonSchema 使用fastjsonschema来校验数据 # 导入验证器 import json import fastjsonschema # 读取schema with open('../ ...