函数重载解构
Destructuring with function overloading
我正在尝试制作一个函数,该函数接受一对坐标或具有 x
和 y
属性以及 returns 邻居列表的对象。但出于某种原因,我无法解构对象,即使我检查了它的类型:
interface Coords {
x: number;
y: number;
}
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
let x: number;
let y: number;
if (typeof a === 'object') {
{ x, y } = a as Coords; // failing
} else {
x = a;
y = b as number;
}
const result = [{ x: x - 1, y }, { x: x + 1, y }, { x, y: y - 1 }, { x, y: y + 1 }];
return result;
}
当然,我可以简单地使用 x = a.x; y = a.y;
,但我很感兴趣 - 我怎样才能使这个解构起作用?
解构之前缺少 let
。
let { x, y } = a as Coords;
需要解构声明和访问全局范围使用 var 而不是 let。
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
// let x: number;
// let y: number;
if (typeof a === 'object') {
var {x, y} = a as Coords; // A destructuring declaration must have an initializer
x = x;
y = y;
console.log('inside destructuring: ', x, y);
} else {
x = a;
y = b as number;
}
console.log('outside destructuring: ', x,y);
const result = [{ x: x - 1, y }, { x: x + 1, y }, { x, y: y - 1 }, { x, y: y + 1 }];
return result;
}
在此处查看StackBlitz example
可以避免 var 并将对象声明为全局..
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
// let x: number;
// let y: number;
let globalStorage: any = {};
if (typeof a === 'object') {
let {x, y} = a as Coords; // A destructuring declaration must have an initializer
globalStorage.x = x;
globalStorage.y = y;
console.log('inside destructuring: ', x, y);
} else {
globalStorage.x = a;
globalStorage.y = b as number;
}
console.log('outside destructuring: ', globalStorage);
const result = [{ x: globalStorage.x - 1, y:globalStorage.y }, { x: globalStorage.x + 1, y:globalStorage.y }, { x: globalStorage.x, y: globalStorage.y - 1 }, { x: globalStorage.x, y: globalStorage.y + 1 }];
return result;
}
好像有点复杂,为什么不把逻辑倒过来,对我来说更具可读性。
console.clear();
class Foo
{
getNeighbours(coords: ICoords): ICoords[];
getNeighbours(x: number, y: number) : ICoords[];
getNeighbours(coords: number | ICoords, y?: number) : ICoords[]{
if (typeof coords !=="object") {
coords = { x: coords, y: y};
}
const result = [
{ x: coords.x - 1, y: coords.y },
{ x: coords.x + 1, y: coords.y },
{ x: coords.x, y: coords.y - 1 },
{ x: coords.x, y: coords.y + 1 }];
return result;
}
}
interface ICoords {
x: number;
y: number;
}
let foo = new Foo();
//debugger;
console.log(foo.getNeighbours(1,1));
console.log(foo.getNeighbours({x:1,y:1}));
我正在尝试制作一个函数,该函数接受一对坐标或具有 x
和 y
属性以及 returns 邻居列表的对象。但出于某种原因,我无法解构对象,即使我检查了它的类型:
interface Coords {
x: number;
y: number;
}
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
let x: number;
let y: number;
if (typeof a === 'object') {
{ x, y } = a as Coords; // failing
} else {
x = a;
y = b as number;
}
const result = [{ x: x - 1, y }, { x: x + 1, y }, { x, y: y - 1 }, { x, y: y + 1 }];
return result;
}
当然,我可以简单地使用 x = a.x; y = a.y;
,但我很感兴趣 - 我怎样才能使这个解构起作用?
解构之前缺少 let
。
let { x, y } = a as Coords;
需要解构声明和访问全局范围使用 var 而不是 let。
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
// let x: number;
// let y: number;
if (typeof a === 'object') {
var {x, y} = a as Coords; // A destructuring declaration must have an initializer
x = x;
y = y;
console.log('inside destructuring: ', x, y);
} else {
x = a;
y = b as number;
}
console.log('outside destructuring: ', x,y);
const result = [{ x: x - 1, y }, { x: x + 1, y }, { x, y: y - 1 }, { x, y: y + 1 }];
return result;
}
在此处查看StackBlitz example
可以避免 var 并将对象声明为全局..
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
// let x: number;
// let y: number;
let globalStorage: any = {};
if (typeof a === 'object') {
let {x, y} = a as Coords; // A destructuring declaration must have an initializer
globalStorage.x = x;
globalStorage.y = y;
console.log('inside destructuring: ', x, y);
} else {
globalStorage.x = a;
globalStorage.y = b as number;
}
console.log('outside destructuring: ', globalStorage);
const result = [{ x: globalStorage.x - 1, y:globalStorage.y }, { x: globalStorage.x + 1, y:globalStorage.y }, { x: globalStorage.x, y: globalStorage.y - 1 }, { x: globalStorage.x, y: globalStorage.y + 1 }];
return result;
}
好像有点复杂,为什么不把逻辑倒过来,对我来说更具可读性。
console.clear();
class Foo
{
getNeighbours(coords: ICoords): ICoords[];
getNeighbours(x: number, y: number) : ICoords[];
getNeighbours(coords: number | ICoords, y?: number) : ICoords[]{
if (typeof coords !=="object") {
coords = { x: coords, y: y};
}
const result = [
{ x: coords.x - 1, y: coords.y },
{ x: coords.x + 1, y: coords.y },
{ x: coords.x, y: coords.y - 1 },
{ x: coords.x, y: coords.y + 1 }];
return result;
}
}
interface ICoords {
x: number;
y: number;
}
let foo = new Foo();
//debugger;
console.log(foo.getNeighbours(1,1));
console.log(foo.getNeighbours({x:1,y:1}));