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

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

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

1. 基本数据类型

  • string: 用于表示字符串。可以使用单引号或双引号包裹文本。
    let s: string = "Hello, World!";
  • number: 用于表示数值,可以是整数或浮点数。
    let n: number = 3.14;
  • boolean: 用于表示布尔值,truefalse
    let b: boolean = true;
  • nullundefined: 分别表示缺失值。
    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/

你可能感兴趣的文章
Objective-C实现haversine distance斜距算法(附完整源码)
查看>>
Objective-C实现heap sort堆排序算法(附完整源码)
查看>>
Objective-C实现heaps algorithm堆算法(附完整源码)
查看>>
Objective-C实现heap堆算法(附完整源码)
查看>>
Objective-C实现Heap堆算法(附完整源码)
查看>>
Objective-C实现hexagonal numbers六边形数算法(附完整源码)
查看>>
Objective-C实现hidden layers neural network浅层神经网络算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现histogram stretch直方图拉伸算法(附完整源码)
查看>>
Objective-C实现Hopcroft算法(附完整源码)
查看>>
Objective-C实现horizontal projectile motion平抛运动算法(附完整源码)
查看>>
Objective-C实现hornerMethod霍纳法算法(附完整源码)
查看>>
Objective-C实现Horn–Schunck光流算法(附完整源码)
查看>>
Objective-C实现Http Post请求(附完整源码)
查看>>
Objective-C实现http下载文件 (附完整源码)
查看>>
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现huffman哈夫曼编码算法(附完整源码)
查看>>
Objective-C实现ID3贪心算法(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>