How to write a object oriented Node.js model
30,722
Solution 1
If I were to design an object like this, then I would have done like this
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
And then it can be used like this
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
Solution 2
I would use a class :
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;
Author by
letter Q
Updated on July 09, 2022Comments
-
letter Q almost 2 years
I am having a lot of trouble writing an object oriented Cat class in Node.js. How can I write a Cat.js class and use it in the following way:
// 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
How would you fix the following Cat.js class I have written:
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; };
-
thefourtheye about 10 years@Chingy I am assuming yours. Try with
==
instead of===
:) -
Asqan about 5 yearsor now better in ES6.
-
Asqan about 5 yearsI came to give the same answer and saw this -4 answer, omg. What's wrong with it?
-
Amir Asyraf almost 3 years@Asqan maybe because the question asks for JS, not TypeScript. And you can't 'use' TypeScript in the browser, it needs to be compiled to JS first.
-
basarat almost 3 yearsI've removed TypeScript from the answer as really modern JavaScript has the feature (classes) I'm recommending.
-
Miftakhul Arzak about 2 yearsHow to use that class on another js file? I've tried
import { Cat } from "./app/models/cat.model.js"
but I got errorCannot use import statement outside a module