/
定义范围(range)的语法为 a..b,其中 a 与 b 是非负整数,例如 2..10。
你可以省略 a 或 b 中的任意一个,此时范围变为开放的。
根据所提供的分隔符数量,范围可以分为:
a..b..ba....开放范围的行为并没有统一定义。每个接受范围的函数都会定义自己的行为。请参阅.read作为一个示例,其策略在标准库的切片操作中很常见。
.. 运算符是 .range {from} {to} 函数的语法糖,区别在于该运算符只接受字面量值。当范围的两端需要动态求值(例如通过数学运算)时,.range 才是合适的选择。
.code
.read {assets/point.ts}export class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}.code
.read {assets/point.ts} lines:{5..7} constructor(x: number, y: number) {
this.x = x;
this.y = y;.code
.read {assets/point.ts} lines:{..3}export class Point {
x: number;
y: number;.code
.read {assets/point.ts} lines:{5..} constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}