javascript - 学习一成不变,听不懂他们说什么
问题描述
我正在研究 immutable.js 的记录。
但是这段代码几乎要了我的命。
我的问题
- 我知道 [import, export,const] 但 [type] 是什么意思。
defaultValues:
,makePoint3D:
,getName(): string
,setName(name: string): this
什么意思。:
除非在 object 或 if 中,否则我从未见过。
这个问题是我理解的关键。
请给我建议!
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);
}
}
解决方案
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
:
推荐阅读
- ios - iOS13的系统字体是什么?
- python - 按类名查找命令不起作用
- java - 如何映射列表
阵列与休眠? - r - 根据列标准对数据框进行排序
- html - Font Awesome 图标未使用 Integrity 属性加载
- javascript - 引导程序中的轮播
- sql - 在 SQL Server 中的 IN 和 JOIN 运算符之间最好使用哪个值列表作为表 2?
- javascript - 带有对象数组的 IE11 探测器(适用于 Firefox 和 Chrome)
- javascript - 如何将选择的日期选择器值从第一页显示到第二页日期选择器
- java - 如何使用 Android Jetpack Navigation 显示自定义 DialogFragment?