Javascript对象可以在构造期间调用自己的方法吗?
我有以下JS,与交互式小说库接口:Javascript对象可以在构造期间调用自己的方法吗?,javascript,Javascript,我有以下JS,与交互式小说库接口: undum.game.situations = { main: new undum.SimpleSituation( "", { enter: function(character, system, from) { ... }, actions: { testMethod: function() { return "h
undum.game.situations = {
main: new undum.SimpleSituation(
"",
{
enter: function(character, system, from) {
...
},
actions: {
testMethod: function() {
return "hello from test method";
},
'test-action': function(character, system, action) {
console.log("testMethod for main situation says: "+this.testMethod());
...
}
}
}
),
...other situation defs
}
当我进入actions的TestAction方法时,控制台显示错误UncaughtTypeError:this.testMethod不是一个函数
我猜问题与testMethod及其调用方被定义为SimpleSituation构造函数的一部分有关?如果是这样的话,我应该如何设置这样的基于事件的代码,其中一个对象在构造期间需要挂接到它自己的方法中,尽管它们在构造期间实际上没有被调用
一个类似的样板,我认为演示相同的分类行为可以很好地工作:
function Obj(optionsObj) {
optionsObj.testMethod();
}
var myObj = new Obj(
{
testMethod: function() {
alert("hello from test method");
}
}
)
理解这个问题的关键是,在JS关键字中,这都是关于调用上下文而不是被调用上下文的-我已经阅读了about关键字this,但它有点误导。在他们的示例代码中
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
它们将全名作为person方法调用,其中关键字this将引用封闭对象。但是,只有当fullName的调用上下文位于person的实例上时才是这样,例如
var p = new person();
p.fullName();
在person实例上被调用使fullName成为一个方法。然而,就其本身而言,fullName与其他任何函数一样只是一个函数,可以从person中提取并用作独立函数:
var fullNameFn = person.fullName;
fullNameFn(); // in this case, keyword this points to global object (usually Window) or undefined in strict mode.
事实证明,我在Undum看到的情况就是这样。它没有调用main.actions.test-action,而是执行以下操作以响应玩家单击操作链接:
SimpleSituation.prototype.act = function(character, system, action) {
// my take-action action function is extracted and stored as response
var response = this.actions[action];
try {
response(character, system, action);
} catch (err) {
if (response) system.write(response);
}
if (this._act) this._act(character, system, action);
};
实际的测试操作函数是从情境实例中提取出来的,并独立执行,导致关键字this指向窗口。理解这个问题的关键是,在JS关键字中,这都是关于调用上下文而不是被调用上下文的-我读过关键字about this,但它有点误导。在他们的示例代码中
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
它们将全名作为person方法调用,其中关键字this将引用封闭对象。但是,只有当fullName的调用上下文位于person的实例上时才是这样,例如
var p = new person();
p.fullName();
在person实例上被调用使fullName成为一个方法。然而,就其本身而言,fullName与其他任何函数一样只是一个函数,可以从person中提取并用作独立函数:
var fullNameFn = person.fullName;
fullNameFn(); // in this case, keyword this points to global object (usually Window) or undefined in strict mode.
事实证明,我在Undum看到的情况就是这样。它没有调用main.actions.test-action,而是执行以下操作以响应玩家单击操作链接:
SimpleSituation.prototype.act = function(character, system, action) {
// my take-action action function is extracted and stored as response
var response = this.actions[action];
try {
response(character, system, action);
} catch (err) {
if (response) system.write(response);
}
if (this._act) this._act(character, system, action);
};
实际的测试操作函数从情境实例中提取并独立执行,导致关键字this指向窗口。在哪里调用测试操作函数?问题涉及调用测试方法函数的方式。我敢打赌,它与声明没有任何关系。@Pointy是的,是关于调用上下文的,它提取了实例方法并将其作为独立函数调用。。。出于某些原因,在哪里调用测试操作函数?问题涉及调用测试方法函数的方式。我敢打赌,它与声明没有任何关系。@Pointy是的,是关于调用上下文的,它提取了实例方法并将其作为独立函数调用。。。出于某种原因