Membership Has Its Privileges (JavaScript)

I have written previously about private methods. It is important to understand how JS deals with private and public versus privileged objects. I plan on doing a post in the future about closures as those are a key way to handle the protection of your code, yet exposing what the program needs. In today’s post, I want to go over privileged methods.

Let’s say we have the following JS constructor…

function Student(grade){
    // private variables
    let gpa = grade * 100; // I know...silly calc example, but not the focus of this post ;)
    this.grade = grade; // make local constructor property equal to grade passed
}

Now, let’s say we want to get the GPA of the student. We could try this…

// Create an instance of the constructor
let student = new Student(75);
// Grab the GPA
student.gpa // What?!? That returns undefined?

The above code will not work because gpa is a private variable property. We need to create a getter that is privileged. We could try this…

// Use prototype to 'add' the method to the constructor
// Function to return gpa grade of student
Student.prototype.gradeReport = function(){
    console.log(this.gpa);
};
student.gradeReport(); // What?!? Still returns undefined!

We are on the right track, but just adding the gradeReport method does not give us access because it is not really a getter. It simply returns what a getter would…well…get. So how do we do this? The protype code is still valid. We need to create the getter code.

Now, full disclosure…we could do this…

// Use prototype to 'add' the method to the constructor
// Function to return gpa grade of student
Student.prototype.gradeReport = function(){
    console.log(this.grade);
};
student.gradeReport();

While that would work, most programs are a tad more complex and it would probably be wiser to create a privileged getter to grab this.grade instead of grabbing it directly in the prototyped method. The reason for this is typically, there is more logic to process inside the constructor for the retrieved property. Here is the change to the constructor…

function Student(grade){
    // private variables
    let gpa = grade * 100; // I know...silly calc example, but not the focus of this post ;)
    this.grade = grade; // make local constructor property equal to grade passed
    // Privileged method property
    this.getCalcGrade= function(){
        return this.grade + '%'; // add a little "logic"
    };
}

Now we can grade the grade property and do what we want to with it outside of the constructor without messing with the internals of the constructor. Here is the complete code…

function Student(grade){
    // private variables
    let gpa = grade * 100; // I know...silly calc example, but not the focus of this post ;)
    this.grade = grade; // make local constructor property equal to grade passed
    // Privileged method property
    this.getCalcGrade= function(){
        return this.grade + '%'; // add a little "logic"
    };
}
Student.prototype.gradeReport = function(){
    console.log(this.getCalcGrade());
};
let student = new Student(75);
student.gradeReport(); // returns 75%

Happy Coding!

Clay Hess

More To Explore

Code

Exploring the Broadcast Channel API: Inter-Tab Communication

Intercommunication between different contexts (like tabs, iframes or workers) of the same origin has often been a challenge. With the Broadcast Channel API, developers now have a powerful tool to easily communicate between browsing contexts. In this blog post, we’ll dive deep into the capabilities of the Broadcast Channel API, exploring its features, use cases, and how it can be effectively implemented in your projects.

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.