简体   繁体   English

如何在这些JS函数中添加OOP?

[英]How can I add OOP to these JS functions?

I know this may seem like a repeated question, but I am currently stuck as to the best way to approach this, limited mostly by my lack of knowledge. 我知道这似乎是一个重复的问题,但我目前仍然坚持最好的方法来解决这个问题,主要是因为我缺乏知识。 Thus I am here to learn. 因此,我在这里学习。

I am trying to do some simple OOP with JavaScript but coming from C# I am having a few issues with how to best solve this problem. 我试图用JavaScript做一些简单的OOP但是来自C#我有一些问题如何最好地解决这个问题。 Below I have four "Classes"; 下面我有四个“课程”; DisplayEngine , DisplayElement , Box , and Grid . DisplayEngineDisplayElementBoxGrid

I would like Box and Grid to inherit DisplayElement , and be able to call the base functions in each of their respective functions. 我希望BoxGrid继承DisplayElement ,并能够在每个函数中调用基函数。 Almost like super.call() or something. 几乎像super.call()或其他东西。

How would you best approach this? 你最好怎么做到这一点?

var DisplayEngine = function() {

    this.elements = [];

    this.add = function(element) {
        this.elements.push(element);
    };

    this.update = function() {
        this.elements.forEach(function(element) {
            element.update();
        })
    };

    this.draw = function() {
        this.elements.forEach(function(element) {
            element.draw();
        })
    };

};

var DisplayElement = function() {

    this.update = function() {
        console.log('DisplayElement update');
    };

    this.draw = function() {
        console.log('DisplayElement draw');
    };

};

var Box = function() {

    this.update = function() {
        console.log('Box update');
        // call DisplayElement.update()
    };

    this.draw = function() {
        console.log('Box draw');
        // call DisplayElement.draw()
    };

};

var Grid = function() {

    this.update = function() {
        console.log('Grid update');
        // call DisplayElement.update()
    };

    this.draw = function() {
        console.log('Grid draw');
        // call DisplayElement.draw()
    };

};

$(function() {
    var displayEngine = new DisplayEngine();
    var box = new Box();
    var grid = new Grid();

    displayEngine.add(box);
    displayEngine.add(grid);
    displayEngine.update();
    displayEngine.draw();
});

Here is a way to do it with prototype , each "class" need to be in his own file, the important part is Grid.prototype = new DisplayElement(); 这是一种用原型做的方法,每个“类”都需要在他自己的文件中,重要的部分是Grid.prototype = new DisplayElement(); This allow you to call function from the DisplayElement in Grid: 这允许您从Grid中的DisplayElement调用函数:

DisplayEngine.js DisplayEngine.js

function DisplayEngine() {

    this.elements = [];

}

DisplayEngine.prototype.add = function(element) {
    this.elements.push(element);
}

DisplayEngine.prototype.update = function() {
    this.elements.forEach(function(element) {
        element.update();
    })
}

DisplayEngine.prototype.draw = function() {
    this.elements.forEach(function(element) {
        element.draw();
    })
}

DisplayElement.js DisplayElement.js

function DisplayElement() {

}

DisplayElement.prototype.updateElement = function() {
    console.log('DisplayElement update');
}

DisplayElement.prototype.drawElement = function() {
    console.log('DisplayElement draw');
}

Box.js Box.js

function Box() {

}

Box.prototype = new DisplayElement();

Box.prototype.update = function() {
    console.log('Box update');
    this.updateElement();
}

Box.prototype.draw = function() {
    console.log('Box draw');
    this.drawElement();
}

Grid.js Grid.js

function Grid() {

}

Grid.prototype = new DisplayElement();

Box.prototype.update = function() {
    console.log('Grid update');
    this.updateElement();
}

Box.prototype.draw = function() {
    console.log('Grid draw');
    this.drawElement();
}

Main.js Main.js

$(function() {
    var displayEngine = new DisplayEngine();
    var box = new Box();
    var grid = new Grid();

    displayEngine.add(box);
    displayEngine.add(grid);
    displayEngine.update();
    displayEngine.draw();
});

To just answer to your question, declare your objects such as : 要回答您的问题,请声明您的对象,例如:

function DisplayElement() {};

DisplayElement.prototype.update = function() {
    console.log('DisplayElement update');
};
DisplayElement.prototype.draw = function() {
    console.log('DisplayElement draw');
};

// ...
// Now, instanciation :
var myElement = new DisplayElement();

Then, for inheritance : 然后,继承:

function Box() {
    DisplayEngine.call(this, arguments); // Call the super constructor
}
Box.prototype = Object.create(DisplayEngine.prototype); // "Apply" the inheritance
Box.prototype.constructor = Box; // Redefine the constructor to Box because it was overriden by the previous line

I disagree about those saying that you doesn't need "classes" in Javascript. 我不同意那些说你在Javascript中不需要“类”的人。 In implementations such as Node.js which will handle datas must have, in my opinion, classes. 在诸如Node.js之类的实现中,在我看来,它将处理数据必须具有的类。 It's easier (always in my opinion) to read, maintain, and use. 阅读,维护和使用更容易(总是在我看来)。

您可以像Shryme解释的那样使用原型样式,或者您可以使用在javascript中模仿经典oop样式的库,检查Classing {js}: http ://www.classingjs.co.nf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM