面试题:JavaScript 类的继承与 this 绑定问题
考虑以下 JavaScript 类继承结构:
```javascript
class Shape {
constructor() {
this.x = 0;
this.y = 0;
}
move(dx, dy) {
this.x += dx;
this.y += dy;
console.log(`Moved to (${this.x}, ${this.y})`);
}
}
class Rectangle extends Shape {
constructor(width, height) {
super();
this.width = width;
this.height = height;
}
draw() {
console.log(`Drawing rectangle at (${this.x}, ${this.y}) with width ${this.width} and height ${this.height}`);
}
}
```
如果有 `const rect = new Rectangle(10, 20);`,然后 `const moveFunction = rect.move.bind({x: 100, y: 100});`,当调用 `moveFunction(5, 5)` 后,再调用 `rect.draw()`,输出是什么?为什么?如何确保 `rect.draw()` 输出正确的位置信息?