如何在另一个类中嵌套一个类?

时间:2012-09-07 23:47:41

标签: javascript class nested

所以我正在尝试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;
}

这是正确的方法吗?我的主要问题是谁在“函数addRoll()”部分拥有“this”对象。我假设“这个”仍然属于动作“类”。我也不确定有关启动新空白对象和使用点表示法分配内容的语法。提前谢谢。

3 个答案:

答案 0 :(得分:1)

忽略函数绑定并调用apply或call,this属性是方法的所有者。主叫...

addRole(...)

this属性指向全局window对象。如果您有一个名为{}的对象new Something()或实例x,其函数为addRole,并将其称为...

x.addRole(...)

this属性为x

附加您已将该功能正确分配给操作对象,以便在您致电时...

var a = new action(...);

然后致电

a.addRole(...);

this属性是您创建的a action个实例。为了防止将其作为全局函数调用并向window添加属性,您可以将其分配给prototypeprototype对象具有一些强大的功能来构建继承,但现在只需更改...

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 = {};
}

答案 1 :(得分:0)

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

你这样做的方式对我来说有点混乱,我通常通过他们的prototype属性

分配构造函数的方法(你称之为类,但不是真的) 像这样

function action()
{
...
}

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

var instance = new action();
instance.addRoll();

答案 2 :(得分: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”对象。