博客
关于我
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中*args 和**kwargs的用法
查看>>
Python中*args和**kwargs的使用
查看>>
python中0、1、True、False、if条件
查看>>
python中argparse模块关于 parse_args() 函数详解(全)
查看>>
python中base64编码与解码
查看>>
python中break怎么用_详解Python中break语句的用法
查看>>
python中del函数的垃圾回收
查看>>
Python中DrissionPage的详细解析与实战
查看>>
python中enumerate、变量类型转换
查看>>
python中execute是啥_python – 为什么“c.execute(…)”打破循环?
查看>>
python中exit和break区别_Python中exit()和sys.exit()之间的区别
查看>>
python中h5的用法
查看>>
python中http协议编程_HTTP编程
查看>>
python中import os_Python:相对导入与绝对导入(import)、os.path、__file__
查看>>
Python中set.discard和set.remove方法之间的运行时差异?
查看>>
python中yield的用法详解——最简单,最清晰的解释
查看>>
python中‘set.intersection()‘的算法是什么?
查看>>
Python中一个图形中的多个绘图
查看>>
python中一个好的持久同步队列
查看>>
Python中不准确的对数
查看>>