如何编写面向对象的 Node.js 模型

新手上路,请多包涵

我在 Node.js 中编写面向对象的 Cat 类时遇到了很多麻烦。如何编写 Cat.js 类并按以下方式使用它:

 // following 10 lines of code is in another file "app.js" that is outside
// the folder "model"
var Cat = require('./model/Cat.js');

var cat1 = new Cat(12, 'Tom');
cat1.setAge(100);
console.log(cat1.getAge()); // prints out 100 to console

var cat2 = new Cat(100, 'Jerry');
console.log(cat1.equals(cat2)); // prints out false

var sameAsCat1 = new Cat(100, 'Tom');
console.log(cat1.equals(sameAsCat1)); // prints out True

您将如何修复我编写的以下 Cat.js 类:

  var Cat = function() {
    this.fields = {
        age: null,
        name: null
    };

    this.fill = function (newFields) {
        for(var field in this.fields) {
            if(this.fields[field] !== 'undefined') {
                this.fields[field] = newFields[field];
            }
        }
    };

    this.getAge = function() {
        return this.fields['age'];
    };

    this.getName = function() {
        return this.fields['name'];
    };

    this.setAge = function(newAge) {
        this.fields['age'] = newAge;
    };

    this.equals = function(otherCat) {
        if (this.fields['age'] === otherCat.getAge() &&
            this.fields['name'] === otherCat.getName())  {
            return true;
        } else {
            return false;
        }
    };
};

module.exports = function(newFields) {
    var instance = new Cat();
    instance.fill(newFields);
    return instance;
};

原文由 letter Q 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 302
2 个回答

如果我要设计这样的对象,那么我会这样做

function Cat(age, name) {       // Accept name and age in the constructor
    this.name = name || null;
    this.age  = age  || null;
}

Cat.prototype.getAge = function() {
    return this.age;
}

Cat.prototype.setAge = function(age) {
    this.age = age;
}

Cat.prototype.getName = function() {
    return this.name;
}

Cat.prototype.setName = function(name) {
    this.name = name;
}

Cat.prototype.equals = function(otherCat) {
    return otherCat.getName() == this.getName()
        && otherCat.getAge() == this.getAge();
}

Cat.prototype.fill = function(newFields) {
    for (var field in newFields) {
        if (this.hasOwnProperty(field) && newFields.hasOwnProperty(field)) {
            if (this[field] !== 'undefined') {
                this[field] = newFields[field];
            }
        }
    }
};

module.exports = Cat;     // Export the Cat function as it is

然后就可以这样使用了

var Cat = require("./Cat.js");

var cat1 = new Cat(12, 'Tom');
cat1.setAge(100);
console.log(cat1.getAge());                 // 100

var cat2 = new Cat(100, 'Jerry');
console.log(cat1.equals(cat2));             // false

var sameAsCat1 = new Cat(100, 'Tom');
console.log(cat1.equals(sameAsCat1));       // true

var sameAsCat2 = new Cat();
console.log(cat2.equals(sameAsCat2));       // false

sameAsCat2.fill({name: "Jerry", age: 100});
console.log(cat2.equals(sameAsCat2));       // true

原文由 thefourtheye 发布,翻译遵循 CC BY-SA 3.0 许可协议

我会使用一个类:

 class Cat {
    fields = {
        age: null,
        name: null
    };

    fill(newFields) {
        for(var field in this.fields) {
            if(this.fields[field] !== 'undefined') {
                this.fields[field] = newFields[field];
            }
        }
    }

    getAge() {
        return this.fields.age;
    }

    setAge(newAge) {
        this.fields.age = newAge;
    }
}

exports.Cat = Cat;

原文由 basarat 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏