HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Defining an OOP structure for a Student class in NodeJS

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
studentnodejsstructureforclassoopdefining

Problem

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?

Solution

In modern versions of node.js (v6.0 or v4.x in strict mode) or when using an ES6 transpiler, you can use the ES6 class keyword:

class Student {
    constructor(name, age, id) {
        // always initialize all instance properties
        this.name = name;
        this.age = age;
        this.id = id;
    }
    getStudentName() {
        return this.name;
    }
    getStudentAge() {
        return this.age;
    }
    getStudentId() {
        return this.id;
    }
}


This creates the same underlying .prototype methods as your original code, but is obviously a bit cleaner syntax. Usage of the Student constructor and methods is identical.

Note: You don't need accessor methods for these properties. They are directly accessible as properties of the object.

If you don't want them directly accessible as properties (e.g. you only want methods to be able to access them), you can do this:

function Student(name, age, id) {
   this.getStudentName = function() {
       return name;
   }
   this.getStudentAge = function() {
       return age;
   }
   this.getStudentID = function() {
       return id;
   }
}


This "hides" the name, age and id instance variables in a constructor closure so they are only accessible to methods defined within the constructor. This does not use the prototype for methods and may (depending upon JS implementation) consume a bit more memory per object, but does give you property privacy.

Code Snippets

class Student {
    constructor(name, age, id) {
        // always initialize all instance properties
        this.name = name;
        this.age = age;
        this.id = id;
    }
    getStudentName() {
        return this.name;
    }
    getStudentAge() {
        return this.age;
    }
    getStudentId() {
        return this.id;
    }
}
function Student(name, age, id) {
   this.getStudentName = function() {
       return name;
   }
   this.getStudentAge = function() {
       return age;
   }
   this.getStudentID = function() {
       return id;
   }
}

Context

StackExchange Code Review Q#136248, answer score: 9

Revisions (0)

No revisions yet.