如果我的键值是像本示例这样的对象,如何从映射对象获取键值?
const map1 = new Map();
map1.set({x:0,y:0}, 'val');
console.log(map1.get({x:0,y:0}));
//output: undefined
我正在创建一个键值的地图 key 是一个点,一个 x 和 y 点的对象 val 是一个生物,因为这个原因等于“测试”。我需要在代码中更改什么才能从该关键对象获取该测试值?
class Board {
constructor() {
this.map = new Map();
}
add(point, creature) {
this.map.set(point, creature)
}
getVal(aX, aY) {
console.log(this.map) // Map { Point { x: 0, y: 0 } => Creature {} }
console.log(new Point(aX, aY)) //output: {x:0,y:0}
console.log(this.map.get(new Point(aX, aY))) //output: undefined
return this.map.get(new Point(aX, aY))
}
}
class Point {
constructor(aX, aY) {
this.x = aX;
this.y = aY;
}
}
function test() {
let board = new Board();
board.add(new Point(0, 0), 'test');
return cretureFromBoard = board.getVal(0, 0);
}
console.log('test()', test())
大话西游666
相关分类