首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Javascript中的函数生成非实例化函数

使用Javascript中的函数生成非实例化函数
EN

Stack Overflow用户
提问于 2014-01-02 09:57:47
回答 2查看 100关注 0票数 5

我有以下代码:

代码语言:javascript
复制
function Rune(){
    this.subSpells = [];
}
function Modifier(){
    this.type = "modifier";
}
Modifier.prototype = new Rune();

function RuneFactory(effect, inheritsFrom, initialValue){
    var toReturn = function(){}; 
    toReturn.prototype = new inheritsFrom();
    toReturn.prototype.subSpells[effect] = initialValue;
    return toReturn;
}

Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);

x = new Duration();
y = new Quicken();

x.subSpells.duration和x.subSpells.quicken都等于1。与y相同。我希望x.subSpells.quicken和y.subSpells.duration都是未定义的。

如果我在持续时间和快速定义中做了以下操作,我就得到了我想要的行为。

代码语言:javascript
复制
Duration = RuneFactory("duration", Rune, 1);
Quicken = RuneFactory("quicken", Rune, 1);

我认为双重继承有问题。有人能告诉我如何更改我的RuneFactory代码,使它能够使用双重继承和/或解释什么是坏的吗?如果可能的话,我想避免使用框架。

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-02 10:16:01

在修饰符函数Rune.apply(this, arguments);中添加下面一行,因为subSpells是在prototype级别定义的,所以Rune的所有实例都指向同一个对象。

并将subSpells的类型从数组更改为对象。

正如所提到的,如果使用寄生组合继承模式,会更好。

“我认为双重继承有问题”--不!

首先,不存在双重继承,您与修饰符构造函数有相同的问题,您的实例引用相同的subSpells对象(在您的情况下是数组)。阅读关于原始值与参考值参考和价值的文章是关于.NET的,但是编程中的基本原则是相同的)。

看看JavaScript .prototype是如何工作的?

看看http://jsfiddle.net/L7dVL/

代码语言:javascript
复制
function inheritPrototype(subType, superType) {
    var prototype = Object.create(superType.prototype, {
        constructor: {
            value: subType,
            enumerable: true
        }
    });

    subType.prototype = prototype;
}

function Rune() {
    this.subSpells = {};
}

function Modifier() {
    Rune.apply(this, arguments);
    this.type = "modifier";
}

inheritPrototype(Modifier, Rune);

function RuneFactory(effect, inheritsFrom, initialValue) {
    function toReturn() {
        inheritsFrom.apply(this, arguments); // you should bind this
        this.subSpells[effect] = initialValue;
    }
    inheritPrototype(toReturn, inheritsFrom);
    return toReturn;
}

Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);

x = new Duration();
y = new Quicken();

console.log(x.subSpells.duration); // 1
console.log(x.subSpells.quicken); // undefined

console.log(y.subSpells.duration); // undefined
console.log(y.subSpells.quicken); // 1
票数 3
EN

Stack Overflow用户

发布于 2014-01-02 10:15:00

在JavaScript中继承的正确方法是:

代码语言:javascript
复制
function Parent(arg1, arg2) {
    // Do something
}

function Child(arg1, arg2) {
    Parent.call(this, arg1, arg2); // or Parent.apply(this, arguments)
}

Child.prototype = Object.create(Parent.prototype, {
    constructor: {
        value: Child
    }
});

所以你需要这样的东西:

代码语言:javascript
复制
function Rune(){
    this.subSpells = {};
}

function Modifier(){
    Rune.apply(this, arguments);
    this.type = "modifier";
}

Modifier.prototype = Object.create(Rune.prototype, {
    constructor: {
        value: Modifier
    }
});

function RuneFactory(effect, inheritsFrom, initialValue){
    var toReturn = function(){}; 
    toReturn.prototype = Object.create(inheritsFrom.prototype, {
        constructor: {
            value: Modifier
        }
    });
    toReturn.prototype.subSpells = {};
    toReturn.prototype.subSpells[effect] = initialValue;
    return toReturn;
}

Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);

x = new Duration();
y = new Quicken();

注意:在您的问题中,.subSpells是一个数组,但是您指定了一个成员,因为它将是一个对象!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20880314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档