在NodeJS中使用JavaScript面向对象编程:如何实现?

13 浏览
0 Comments

在NodeJS中使用JavaScript面向对象编程:如何实现?

我习惯于使用Java中的经典面向对象编程(OOP)方式。

在使用NodeJS的JavaScript中进行OOP编程的最佳实践是什么?

每个类都是一个带有module.export的文件吗?

如何创建类?

this.Class = function() {
    //构造函数?
    var privateField = ""
    this.publicField = ""
    var privateMethod = function() {}
    this.publicMethod = function() {} 
}

对比之下(我甚至不确定它是否正确):

this.Class = {
    privateField: ""
    , privateMethod: function() {}
    , return {
        publicField: ""
        publicMethod: function() {}
    }
}

对比之下:

this.Class = function() {}
this.Class.prototype.method = function(){}
...

继承如何工作?

有没有专门用于在NodeJS中实现OOP的模块?

我发现有很多不同的方式来创建类似于OOP的东西...但我不知道最常用/实用/清晰的方式是什么。

额外问题:在使用MongooseJS时,建议使用什么样的“OOP风格”?(可以将MongooseJS文档视为类,使用模型作为实例吗?)

编辑

这里有一个JsFiddle的例子,请提供反馈。

//http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/
function inheritPrototype(childObject, parentObject) {
    var copyOfParent = Object.create(parentObject.prototype)
    copyOfParent.constructor = childObject
    childObject.prototype = copyOfParent
}
//例子
function Canvas (id) {
    this.id = id
    this.shapes = {} //改为对象?
    console.log("Canvas constructor called "+id)
}
Canvas.prototype = {
    constructor: Canvas
    , getId: function() {
        return this.id
    }
    , getShape: function(shapeId) {
        return this.shapes[shapeId]
    }
    , getShapes: function() {
        return this.shapes
    }
    , addShape: function (shape)  {
        this.shapes[shape.getId()] = shape
    }
    , removeShape: function (shapeId)  {
        var shape = this.shapes[shapeId]
        if (shape)
            delete this.shapes[shapeId]
        return shape
    }
}
function Shape(id) {
    this.id = id
    this.size = { width: 0, height: 0 }
    console.log("Shape constructor called "+id)
}
Shape.prototype = {
    constructor: Shape
    , getId: function() {
        return this.id
    }
    , getSize: function() {
        return this.size
    }
    , setSize: function (size)  {
        this.size = size
    }
}
//继承
function Square(id, otherSuff) {
    Shape.call(this, id) //相当于 Shape.prototype.constructor.apply( this, arguments ); ?
    this.stuff = otherSuff
    console.log("Square constructor called "+id)
}
inheritPrototype(Square, Shape)
Square.prototype.getSize = function() { //覆盖
    return this.size.width
}
function ComplexShape(id) {
    Shape.call(this, id)
    this.frame = null
    console.log("ComplexShape constructor called "+id)
}
inheritPrototype(ComplexShape, Shape)
ComplexShape.prototype.getFrame = function() {
    return this.frame
}
ComplexShape.prototype.setFrame = function(frame) {
    this.frame = frame
}
function Frame(id) {
    this.id = id
    this.length = 0
}
Frame.prototype = {
    constructor: Frame
    , getId: function() {
        return this.id
    }
    , getLength: function() {
        return this.length
    }
    , setLength: function (length)  {
        this.length = length
    }
}
/////运行
var aCanvas = new Canvas("c1")
var anotherCanvas = new Canvas("c2")
console.log("aCanvas: "+ aCanvas.getId())
var aSquare = new Square("s1", {})
aSquare.setSize({ width: 100, height: 100})
console.log("square overridden size: "+aSquare.getSize())
var aComplexShape = new ComplexShape("supercomplex")
var aFrame = new Frame("f1")
aComplexShape.setFrame(aFrame)
console.log(aComplexShape.getFrame())
aCanvas.addShape(aSquare)
aCanvas.addShape(aComplexShape)
console.log("Shapes in aCanvas: "+Object.keys(aCanvas.getShapes()).length)
anotherCanvas.addShape(aCanvas.removeShape("supercomplex"))
console.log("Shapes in aCanvas: "+Object.keys(aCanvas.getShapes()).length)
console.log("Shapes in anotherCanvas: "+Object.keys(anotherCanvas.getShapes()).length)
console.log(aSquare instanceof Shape)
console.log(aComplexShape instanceof Shape)

0