Most of my coding experience is in C#. The way object oriented programming is laid out in C# is a bit different from NodeJS, hence this is my first NodeJS OOP for a Student class.
I'm trying to translate this C# structure of creating a class, object and methods to NodeJS:
class Student { private int _age; public int Age { get { return _age; } set { _age = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } private string _id; public string ID { get { return _id; } set { _id = value; } } }
Sample usage:
Student student = new Student(); student.Age = 12; student.Name= "Tolani"; student.ID = "Pokemon1234";
NodeJS code:
// Constructor
function Student(name, age, id)
{
// always initialize all instance properties
this.name = name;
this.age = age;
this.id = id;
}
// Get the student Name
Student.prototype.getStudentName = function()
{
return this.name;
};
// Gets the student Age
Student.prototype.getStudentAge = function()
{
return this.age;
};
// Gets the student's ID
Student.prototype.getStudentId = function()
{
return this.id;
};
// export the class
module.exports = Student;
var student = new Student('Tolani', 23, 'ddr1234');
console.log('The student name is ' + student.getStudentName());
In general, can this be improved?