Super Class 属性 未定义尽管有 Super 调用
Super Class Property Undefined Despite Super Call
我正在尝试使用 ECMAScript 6 编写文本冒险游戏,并使用 Babel 转换为 ECMAScript 5。我 运行 遇到了一个奇怪的情况,它没有出现在我的代码中的其他地方。
我有一个通用操作 class 可以解析命令,确定给定输入是否与触发操作的命令匹配,然后调用函数来实际执行操作。
/**
* An action represents a mapping between a string and some kind of action in
* the system.
*/
export class Action {
/**
* Construct a new action instance.
*/
constuctor () {
this.commands = [];
}
/**
* Add a command as a trigger for this action.
*
* @param {String} command The command string to add as a trigger.
*
* @returns {Boolean} True if the command was added, false otherwise.
*/
addCommand (command) {
var paramRegex = /\{([a-zA-Z0-9_]+)\}/g;
var i = 0;
var regexResult;
var commandRegex;
var parameters;
if (typeof command !== 'string' || command.length < 1) {
return false;
}
parameters = {};
// Make spaces generic
commandRegex = command.replace(/\w+/g, '\w+');
regexResult = paramRegex.exec(commandRegex);
while (regexResult) {
parameters[i] = regexResult[1];
commandRegex = commandRegex.replace(regexResult[0], '([^\w]+)');
i++;
regexResult = paramRegex.exec(commandRegex);
}
this.commands.push({
regex: new RegExp(commandRegex, 'gi'),
parameters: parameters
});
return true;
}
/**
* Determine if the action has a trigger matching the given command string.
*
* @param {String} command The command to parse.
*
* @returns {Boolean} True if the command triggers the action, false
* otherwise.
*/
isTrigger (command) {
var i;
for (i = 0; i < this.commands.length; i++) {
if (this.commands[i].regex.test(command)) {
return true;
}
}
return false;
}
/**
* Trigger the action using the given command. This method checks if the
* command is a trigger for the action, extracts any arguments from the
* command string, and passes them to the {@link Action#execute} function
* using {@link Function#apply}.
*
* @param {String} command The command to use to trigger the action.
*
* @returns {Boolean} True if the trigger was successful, false otherwise.
*/
trigger (command) {
var args;
var result;
var i;
for (i = 0; i < this.commands.length; i++) {
result = this.commands[i].regex.exec(command);
if (result != null) {
break;
}
}
if (result == null) {
return false;
}
args = [];
for (i = 1; i < result.length; i++) {
args.push(result[i]);
}
this.execute.apply(this, args);
}
/**
* Execute the action with the given arguments.
*
* @returns {Boolean} True if the execution was successful, false otherwise.
*
* @virtual
*/
execute () {
throw new Error('Not implemented');
}
}
我有一个子class,它应该执行移动动作并将玩家从一个位置移动到另一个位置。
/**
* An action that moves the player.
*
* @extends {Action}
*/
export class MoveAction extends Action {
constructor () {
super();
this.addCommand('go to {place}');
this.addCommand('move to {place}');
this.addCommand('go {place}');
}
execute (place) {
var loc = GameObject.getGameObject(place);
if (!loc) {
return false;
}
return player.setLocation(loc);
}
}
我在我的代码上 运行 Babel 以生成兼容的 ECMAScript 5 代码,并尝试通过 Node.JS 手动 运行 它们以验证它们的工作,然后再将它们推送到我的 Git repository(我打算以后写测试用例)。
不过,我 运行 遇到了一个问题,其中 Node.JS 抱怨 this.commands
未定义。
> var MoveAction = require('./compiled/actions/moveAction').MoveAction;
undefined
> var act = new MoveAction();
TypeError: Cannot read property 'push' of undefined
at MoveAction.addCommand (compiled\action.js:64:26)
at new MoveAction (compiled\actions\moveAction.js:41:39)
at repl:1:11
at REPLServer.defaultEval (repl.js:248:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
我无法立即发现我所写的任何错误,并且我在当前代码的其他地方做了类似的事情,没有任何问题。我将继续研究它,看看我是否可以自己解决这个问题,但如果有人提出可以节省我时间的建议,我将不胜感激。
解决了,我把constructor
拼错为constuctor
。
我正在尝试使用 ECMAScript 6 编写文本冒险游戏,并使用 Babel 转换为 ECMAScript 5。我 运行 遇到了一个奇怪的情况,它没有出现在我的代码中的其他地方。
我有一个通用操作 class 可以解析命令,确定给定输入是否与触发操作的命令匹配,然后调用函数来实际执行操作。
/**
* An action represents a mapping between a string and some kind of action in
* the system.
*/
export class Action {
/**
* Construct a new action instance.
*/
constuctor () {
this.commands = [];
}
/**
* Add a command as a trigger for this action.
*
* @param {String} command The command string to add as a trigger.
*
* @returns {Boolean} True if the command was added, false otherwise.
*/
addCommand (command) {
var paramRegex = /\{([a-zA-Z0-9_]+)\}/g;
var i = 0;
var regexResult;
var commandRegex;
var parameters;
if (typeof command !== 'string' || command.length < 1) {
return false;
}
parameters = {};
// Make spaces generic
commandRegex = command.replace(/\w+/g, '\w+');
regexResult = paramRegex.exec(commandRegex);
while (regexResult) {
parameters[i] = regexResult[1];
commandRegex = commandRegex.replace(regexResult[0], '([^\w]+)');
i++;
regexResult = paramRegex.exec(commandRegex);
}
this.commands.push({
regex: new RegExp(commandRegex, 'gi'),
parameters: parameters
});
return true;
}
/**
* Determine if the action has a trigger matching the given command string.
*
* @param {String} command The command to parse.
*
* @returns {Boolean} True if the command triggers the action, false
* otherwise.
*/
isTrigger (command) {
var i;
for (i = 0; i < this.commands.length; i++) {
if (this.commands[i].regex.test(command)) {
return true;
}
}
return false;
}
/**
* Trigger the action using the given command. This method checks if the
* command is a trigger for the action, extracts any arguments from the
* command string, and passes them to the {@link Action#execute} function
* using {@link Function#apply}.
*
* @param {String} command The command to use to trigger the action.
*
* @returns {Boolean} True if the trigger was successful, false otherwise.
*/
trigger (command) {
var args;
var result;
var i;
for (i = 0; i < this.commands.length; i++) {
result = this.commands[i].regex.exec(command);
if (result != null) {
break;
}
}
if (result == null) {
return false;
}
args = [];
for (i = 1; i < result.length; i++) {
args.push(result[i]);
}
this.execute.apply(this, args);
}
/**
* Execute the action with the given arguments.
*
* @returns {Boolean} True if the execution was successful, false otherwise.
*
* @virtual
*/
execute () {
throw new Error('Not implemented');
}
}
我有一个子class,它应该执行移动动作并将玩家从一个位置移动到另一个位置。
/**
* An action that moves the player.
*
* @extends {Action}
*/
export class MoveAction extends Action {
constructor () {
super();
this.addCommand('go to {place}');
this.addCommand('move to {place}');
this.addCommand('go {place}');
}
execute (place) {
var loc = GameObject.getGameObject(place);
if (!loc) {
return false;
}
return player.setLocation(loc);
}
}
我在我的代码上 运行 Babel 以生成兼容的 ECMAScript 5 代码,并尝试通过 Node.JS 手动 运行 它们以验证它们的工作,然后再将它们推送到我的 Git repository(我打算以后写测试用例)。
不过,我 运行 遇到了一个问题,其中 Node.JS 抱怨 this.commands
未定义。
> var MoveAction = require('./compiled/actions/moveAction').MoveAction;
undefined
> var act = new MoveAction();
TypeError: Cannot read property 'push' of undefined
at MoveAction.addCommand (compiled\action.js:64:26)
at new MoveAction (compiled\actions\moveAction.js:41:39)
at repl:1:11
at REPLServer.defaultEval (repl.js:248:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
我无法立即发现我所写的任何错误,并且我在当前代码的其他地方做了类似的事情,没有任何问题。我将继续研究它,看看我是否可以自己解决这个问题,但如果有人提出可以节省我时间的建议,我将不胜感激。
解决了,我把constructor
拼错为constuctor
。