1

因此,我正在尝试使用 JavaScript“类”来尝试澄清和简化我的一些代码。我所拥有的是这样的:

function action (name, vActor, vTarget) {
    this.name = name;
    this.vActor = vActor;
    this.vTarget = vTarget;
    this.addRoll = addRoll;
    this.children = {};
}
function addRoll (name, diffMod, dice, success, critSuccess, failure) {
    this.children[name] = {} ;
    this.children[name].diffMod = diffMod;
    this.children[name].dice = dice;
    this.children[name].success =       {condition: success,        outcome: {}};
    this.children[name].critSuccess =   {condition: critSuccess,    outcome: {}};
    this.children[name].failure =       {condition: failure,        outcome: {}};
    this.children[name].addSuccess = addSuccess;
    this.children[name].addFailure = addFailure;
    this.children[name].addOutcome = addOutcome;
}

这是解决这个问题的正确方法吗?我的主要问题是关于谁拥有“function addRoll()”部分中的“this”对象。我假设“这个”仍然属于动作“类”。我也不确定关于开始一个新的空白对象和使用点符号分配东西的语法。提前致谢。

4

3 回答 3

1

忽略函数绑定和调用apply或call,this属性是方法的所有者。打电话...

addRole(...)

this属性指向全局window对象。如果你有一个用函数调用的对象{}或实例并调用它......new Something()xaddRole

x.addRole(...)

this物业是x

附加您已将功能正确分配给操作对象,因此当您调用...

var a = new action(...);

然后打电话

a.addRole(...);

this属性是您创建的a实例。action为了防止它被称为全局函数并向 中添加属性,window您可以将其分配给prototype. 该prototype对象具有一些强大的功能来构建继承,但现在只是改变......

addRole(...) {...}

对以下...

action.prototype.addRole = function(...) {...}

并删除正在执行的任务......

this.addRole = addRole

将防止函数在没有所有者的情况下被意外调用

此外,您可以重写在 addRole 中分配子项的方式,以更好地使用对象文字表示法......

function addRoll(name, diffMod, dice, success, critSuccess, failure) {
    this.children[name] = {
        diffMod: diffMod,
        dice: dice,
        success: {
            condition: success,
            outcome: {}
        },
        critSuccess: {
            condition: critSuccess,
            outcome: {}
        },
        failure: {
            condition: failure,
            outcome: {}
        },
        addSuccess: addSuccess,
        addFailure: addFailure,
        addOutcome: addOutcome
    };
}

您还可以重构代码以使用子类的类,如下所示。

function Action(name, vActor, vTarget) {
    this.name = name;
    this.vActor = vActor;
    this.vTarget = vTarget;
    this.children = {};
}
Action.prototype.addRoll = function(name, role) {
    this.children[name] = role;
}

function Role(diffMod, dice, success, critSuccess, failure) {
    this.diffMod = diffMod;
    this.dice = dice;
    this.success = success;
    this.critSuccess = critSuccess;
    this.failure = failure;
}
Role.prototype.addSuccess = function(...) {...}
Role.prototype.addFailure = function(...) {...}
Role.prototype.addOutcome = function(...) {...}

function Condition(condition) {
    this.condition = condition;
    this.outcome = {};
}
于 2012-09-08T00:05:10.477 回答
0

您在这里问了很多问题,让我尝试一次回答一个,这是您的代码,简化版

function Action (name) {
    this.name = name;
    this.addRoll = addRoll;
}
function addRoll (name, diffMod, dice, success, critSuccess, failure) {
    ...
}
  1. 如何开始一个新的空白对象?
    你使用“新”关键字,所以要创建一个新的“动作”对象,你做var newAction = new Action('hello')

  2. javascript中的“this”指的是什么?
    “this”指的是函数的调用者。一个简单的例子:
    var newAction = new Action('hello world');
    newAction.addRoll(//...);
    在这个例子中,addRoll 函数中的“this”是“newAction”
    ,但是如果你直接调用“addRoll”,如果你使用浏览器,“this”指的是“Window”对象。

于 2012-09-08T00:07:54.797 回答
0

是的,如果addRoll从 的实例调用actionthenthis对象仍将属于action.

prototype你这样做的方式对我来说有点困惑,我通常通过它们的属性分配构造函数的方法(你称之为类,但不是真的)

像这样

function action()
{
...
}

action.prototype.addRoll = function(){...}

var instance = new action();
instance.addRoll();
于 2012-09-07T23:57:48.000 回答