博客
关于我
TypeScript知识点简单汇总
阅读量:327 次
发布时间:2019-03-04

本文共 2942 字,大约阅读时间需要 9 分钟。

数据类型是 JavaScript 中的核心概念,它决定了变量的类型和值的范围。以下是 JavaScript 中常见的数据类型及其使用方法。

1. 基本数据类型

  • string: 用于表示字符串。可以使用单引号或双引号包裹文本。
    let s: string = "Hello, World!";
  • number: 用于表示数值,可以是整数或浮点数。
    let n: number = 3.14;
  • boolean: 用于表示布尔值,true 或 false。
    let b: boolean = true;
  • null 和 undefined: 分别表示缺失值。
    let variable: string;variable = null; // 变量被赋值为 null
  • symbol: 用于表示独特的标识符,常用于避免命名冲突。
    const symbol: symbol = Symbol('mySymbol');

2. 数组

数组是数据类型的扩展,用于存储多个同一类型的元素。

// 创建字符串数组let arr1: string[] = ['a', 'b', 'c'];// 创建数值数组let arr2: Array
= [1, 2, 3, 4, 5];// 创建元祖let tuple: [string, number] = ['a', 1];// 创建带有联合类型的数组let arr3: (string | number)[] = ['a', 1];// 使用 any 类型创建灵活数组let anyList: any[] = ['a', 1, true];

3. 枚举

枚举类型允许代码更安全地处理常量。

enum Directions {    left = 2,    right,    top,    bottom}console.log(Directions.top); // 输出: topconsole.log(Directions[2]); // 输出: left

4. 对象

对象是 JavaScript 中用于创建复杂数据结构的核心。

interface LabelledValue {    label: string;}function printLabel(labelledObj: LabelledValue) {    console.log(labelledObj.label);}let myObj = { size: 10, label: "Size 10 Object" };printLabel(myObj);

5. 函数

函数是 JavaScript 中的核心概念,用于定义代码逻辑。

function add(x: number, y: number): number {    return x + y;}let myAdd = function(x: number, y: number): number {    return x + y;}

6. 类

类与接口结合使用,用于构建复杂的对象结构。

class Person {    name: string;    age: number;    constructor(name: string, age: number) {        this.name = name;        this.age = age;    }    getName(): string {        return this.name;    }}

7. 抽象类

抽象类是类的一种特殊形式,用于定义抽象属性和方法。

abstract class Animal {    abstract type: string;    name: string;    constructor(name: string) {        this.name = name;    }    abstract getType(): string;    getName(): string {        return this.name;    }}class Dog extends Animal {    type: string;    constructor(name: string, type: string) {        super(name);        this.type = type;    }    getType(): string {        return this.type;    }}

8. 接口

接口用于定义对象的公共属性和方法的契约。

interface Girl {    name: string;    age: number;    loveYou(): void;}class GrilFriend implements Girl {    name: string;    age: number;    constructor(name: string, age: number) {        this.name = name;        this.age = age;    }    loveYou(): void {        console.log("Her name is " + this.name + ", her age is " + this.age);    }}let gf = new GrilFriend("Linda", 28);gf.loveYou();

9. 泛型

泛型是 JavaScript 中的高级特性,用于创建可扩展的函数和类。

// 泛型函数function showData
(person: T): T { return person;}showData
("Lucy");// 泛型函数接口interface GenericIdentityFn { (arg: T): T;}function identity(arg: T): T { return arg;}let myIdentity: GenericIdentityFn = identity;

10. 泛型类

泛型类允许类的实例部分使用任意类型。

class GenericNumber
{ zeroValue: T; add: (x: T, y: T) => T;}let myGenericNumber = new GenericNumber
();myGenericNumber.zeroValue = 0;myGenericNumber.add = function(x, y) { return x + y;};

通过以上内容,可以清晰地了解 JavaScript 中的数据类型、函数、类和接口等核心概念,以及如何利用这些概念构建复杂的应用程序。

转载地址:http://movq.baihongyu.com/

你可能感兴趣的文章
python三大库之--numpy(二)
查看>>
python上的字无效字符_标识中的无效字符
查看>>
python下用decode和encode 中文转码
查看>>
Python下调用Linux的Shell命令
查看>>
Python接口自动化测试实战系列
查看>>
python下载_安装_配置_以及第一行python程序---python工作笔记009
查看>>
Python不协调机器人消息.内容
查看>>
Python与C#之间内存共享互传信息
查看>>
python与c语言混合编程_你试过C语言和Python一起混合编程吗?两者相加不是已经无敌了!...
查看>>
python与excel
查看>>
Python与Excel的完美邂逅:深入浅出,手把手教你轻松玩转Excel数据操作
查看>>
python与flask_Python编程中flask的简介与简单使用
查看>>
python与图灵机器人交互(WXPY版本)
查看>>
python与运算符_python运算符
查看>>
python两个列表获取交集,并集,差集
查看>>
python两个字典合并,两个list合并
查看>>
Python两个版本共存时,命令行升级pip
查看>>
Python两个版本共存时,怎么验证是否安装成功 scrapy
查看>>
Python两个版本共存时,通过pip安装Scrapy
查看>>
python两组变量的相关关系散点图_变量是什么?变量间的两种关系——函数关系和相关关系(小学版)...
查看>>