如何编写一个面向对象的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; }; 

如果我要devise一个这样的对象,那么我会这样做

 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 

这段代码在这里工作正常Person.js代码

  exports.person=function(age,name) { age=age; name=name; this.setAge=function(agedata) { age=agedata; } this.getAge=function() { return age; } this.setName=function(name) { name=name; } this.getName=function() { return name; } }; 

调用对象代码:

 var test=require('./route/person.js'); var person=test.person; var data=new person(12,'murugan'); data.setAge(13); console.log(data.getAge()); data.setName('murugan'); console.log(data.getName()); 

我会使用TypeScript:

 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:number) { this.fields.age = newAge; } } export = Cat; 

你可以看到它生成的JavaScript。

TypeScript可以在NodeJS和Browser中使用。