如何在 javascript 中管理 super() 构造函数参数
how to manage super() constructor parameters in javascript
这是来自mazeContainer.js的一小段代码,只有必要的部分-
import Cell from "./cell.js";
import Player from "./player.js";
export default class Maze {
....
setup(){
for (let rowNum = 0; rowNum < this.rows; rowNum++) {
let row = [];
for (let colNum = 0; colNum < this.columns; colNum++) {
let cell = new Cell(this.ctx, rowNum, colNum, this.cellWidth, this.cellHeight);
row.push(cell);
}
this.grid.push(row);
}
drawMap(){
....
let player = new Player(this.goal, this.lastRow, this.lastColumn);
....
}
}
和player.js-
import Cell from "./cell.js";
export default
class Player extends Cell {
constructor(goal, lastRow, lastColumn) {
super(); // need to manage this statement
this.goal = goal;
this.lastRow = lastRow;
this.lastColumn = lastColumn;
}
....
}
这就是我遇到的问题。
我刚遇到 super
关键字,到目前为止我所知道的是我需要在使用 this
之前调用 super
方法。那不是问题。但是这里我还需要为Cell
的构造函数提供所有参数。
如你所见,Cell
class的构造函数中有很多参数,如何将它们传递给new Player(....)
?
有没有更好的方法来实现这个?
• extends 关键字使动物 class 的方法在猫 class 中可用。
• 创建新 Cat 对象时调用的构造函数接受两个参数,name 和 usesLitter。
• super 关键字调用父class 的构造函数。在这种情况下,super(name) 将 Cat class 的名称参数传递给 Animal class 的构造函数。当 Animal 构造函数运行时,它设置 this._name = name;对于新的 Cat 实例。
• _usesLitter 是 Cat class 独有的新 属性,因此我们在 Cat 构造函数中设置它。
class Animal {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
class Cat extends Animal {
constructor(name, usesLitter) {
super(name); // The super keyword calls the constructor of the parent class.
this._usesLitter = usesLitter;
}
get usesLitter() {
return this._usesLitter;
}
}
const bryceCat = new Cat('Bryce', true);
console.log(bryceCat.name); //Output: Bryce
console.log(bryceCat.usesLitter); //Output: true
这是来自mazeContainer.js的一小段代码,只有必要的部分-
import Cell from "./cell.js";
import Player from "./player.js";
export default class Maze {
....
setup(){
for (let rowNum = 0; rowNum < this.rows; rowNum++) {
let row = [];
for (let colNum = 0; colNum < this.columns; colNum++) {
let cell = new Cell(this.ctx, rowNum, colNum, this.cellWidth, this.cellHeight);
row.push(cell);
}
this.grid.push(row);
}
drawMap(){
....
let player = new Player(this.goal, this.lastRow, this.lastColumn);
....
}
}
和player.js-
import Cell from "./cell.js";
export default
class Player extends Cell {
constructor(goal, lastRow, lastColumn) {
super(); // need to manage this statement
this.goal = goal;
this.lastRow = lastRow;
this.lastColumn = lastColumn;
}
....
}
这就是我遇到的问题。
我刚遇到 super
关键字,到目前为止我所知道的是我需要在使用 this
之前调用 super
方法。那不是问题。但是这里我还需要为Cell
的构造函数提供所有参数。
如你所见,Cell
class的构造函数中有很多参数,如何将它们传递给new Player(....)
?
有没有更好的方法来实现这个?
• extends 关键字使动物 class 的方法在猫 class 中可用。
• 创建新 Cat 对象时调用的构造函数接受两个参数,name 和 usesLitter。
• super 关键字调用父class 的构造函数。在这种情况下,super(name) 将 Cat class 的名称参数传递给 Animal class 的构造函数。当 Animal 构造函数运行时,它设置 this._name = name;对于新的 Cat 实例。
• _usesLitter 是 Cat class 独有的新 属性,因此我们在 Cat 构造函数中设置它。
class Animal {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
class Cat extends Animal {
constructor(name, usesLitter) {
super(name); // The super keyword calls the constructor of the parent class.
this._usesLitter = usesLitter;
}
get usesLitter() {
return this._usesLitter;
}
}
const bryceCat = new Cat('Bryce', true);
console.log(bryceCat.name); //Output: Bryce
console.log(bryceCat.usesLitter); //Output: true