【问题标题】:studying immutable and can't understand what they say学习一成不变,听不懂他们说什么
【发布时间】:2020-10-21 10:28:44
【问题描述】:

我正在研究 immutable.js 的记录。

但这段代码几乎要了我的命。

我的问题

  1. 我知道 [import, export,const] 但 [type] 是什么意思。

  2. defaultValues: makePoint3D:getName(): stringsetName(name: string): this 是什么意思。我从未见过:,除非在对象中或如果。

这个问题是我理解的关键。

请给我建议!

import type { RecordFactory, RecordOf } from 'immutable';

// Use RecordFactory<TProps> for defining new Record factory functions.
type Point3DProps = { x: number, y: number, z: number };
const defaultValues: Point3DProps = { x: 0, y: 0, z: 0 };
const makePoint3D: RecordFactory<Point3DProps> = Record(defaultValues);
export makePoint3D;

// Use RecordOf<T> for defining new instances of that Record.
export type Point3D = RecordOf<Point3DProps>;
const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });

type PersonProps = {name: string, age: number};
const defaultValues: PersonProps = {name: 'Aristotle', age: 2400};
const PersonRecord = Record(defaultValues);
class Person extends PersonRecord<PersonProps> {
  getName(): string {
    return this.get('name')
  }

  setName(name: string): this {
    return this.set('name', name);
  }
}

来自https://immutable-js.github.io/immutable-js/docs/#/Record

【问题讨论】:

  • getName(): string , getName() 是您的方法,: 之后的部分讲述了返回类型。基本上 : 之后的部分通常表示大约 type 。返回类型,变量类型等无论你使用什么
  • 这种奇葩方式只有js?
  • 是的,我们在打字稿中以这种方式使用

标签: javascript typescript record immutable.js


【解决方案1】:

getName(): string , getName() 是你的方法,: 之后的部分讲述了返回类型。基本上: 之后的部分通常表示关于 type 。返回类型,变量类型等,无论你使用什么

getName() : string
{
return "Hi";       
}

getName() : string
{
 let a = 2;
 return a;
}

第二个方法将给出error,因为我们的方法返回类型是string,而我们返回的是Number

有时我们在if这样的条件下使用:这个

 isGendeMale : boolean = false;
 var gender : string = isGendeMale ? "Male" : "Female";

说明

? 如果条件为 true 则检查值,如果 false 将分配 : 之后的部分,则将分配 : 之前的部分

Colon vs Equal

Basic Types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2019-09-06
    • 2021-02-23
    • 2022-01-09
    • 2011-12-18
    • 2020-07-06
    • 2011-04-15
    • 2017-01-30
    相关资源
    最近更新 更多