Implement Triplet class in JavaScript
This post will discuss some of the possible ways to implement a Triplet class in JavaScript.
A Triplet class is a data structure that represents a tuple with three values of any type. It can be used to store and manipulate three related values, such as coordinates, colors, or numbers, or any other combination of three related values. There are different ways to implement a Triplet class in JavaScript. Here are some possible functions:
1. Using an object
A simple and convenient way to implement a Triplet class is to use an object with three properties. This allows more descriptive names for the elements and avoids using magic numbers for the indexes. However, it may require more code to access and modify the elements using dot notation. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// Define the triplet class class Triplet { // Constructor that takes three values as parameters constructor(first, second, third) { this.first = first; this.second = second; this.third = third; } // getters getFirst() { return this.first; } getSecond() { return this.second; } getThird() { return this.third; } // setters setFirst(first) { this.first = first; } setSecond(second) { this.second = second; } setThird(third) { this.third = third; } // Function to convert the triplet to a string toString() { return `(${this.first}, ${this.second}, ${this.third})`; } } // Create an instance of the triplet class let t = new Triplet(1, "Hello", true); // Test the functions console.log(t.getFirst()); // 1 console.log(t.getSecond()); // Hello console.log(t.getThird()); // true console.log(t.toString()); // (1, Hello, true) t.setFirst(2); t.setSecond("World"); t.setThird(false); console.log(t); // Triplet { first: 2, second: 'World', third: false } |
2. Using an array
Another way to implement a Triplet class is to use an array with three elements. This allows easy access and modification of the elements using the array index or functions like get(), set(), toString(), etc. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class Triplet { constructor(first, second, third) { // create an array with three elements this.data = [first, second, third]; } get(index) { // return the element at the given index return this.data[index]; } set(index, value) { // assign the value to the element at the given index this.data[index] = value; } toString() { // return a string representation of the triplet return "(" + this.data.join(", ") + ")"; } } // Create an instance of the triplet class let t = new Triplet(1, "Hello", true); // Test the functions console.log(t.get(0)); // 1 console.log(t.get(1)); // Hello console.log(t.get(2)); // true console.log(t.toString()); // (1, Hello, true) t.set(0, 2); t.set(1, "World"); t.set(2, false); console.log(t); // Triplet {data: [2, 'World', false]} |
3. Using a function
A third way to implement a Triplet class is to use a function that returns an object with three properties. This allows more flexibility and customization of the triplet object, such as adding functions or getters and setters. However, it may require more memory and complexity to create and manage the triplet objects. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
function Triplet(first, second, third) { // create an empty object let obj = {}; // assign three properties to the object obj.first = first; obj.second = second; obj.third = third; // add a function to get property first obj.getFirst = function() { return obj.first; }; // add a function to get property second obj.getSecond = function() { return obj.second; }; // add a function to get property third obj.getThird = function() { return obj.third; }; // add a getter for the sum of the properties Object.defineProperty(obj, "sum", { get: function() { return obj.first + obj.second + obj.third; } }); // add a function to return a string representation of the object obj.toString = function() { return "(" + obj.first + ", " + obj.second + ", " + obj.third + ")"; }; // return the object return obj; } // Create an instance of the triplet class let t = Triplet(1, "Hello", true); // Test the functions console.log(t.getFirst()); // 1 console.log(t.getSecond()); // Hello console.log(t.getThird()); // true console.log(t.toString()); // (1, Hello, true) t.first = 2; t.second = "World"; t.third = false; console.log(t); // { first: 2, second: 'World', third: false, /*...*/ } |
4. Using prototype functions
One way to implement a Triplet class in JavaScript is to use a constructor function and prototype functions. Here is an example of a Triplet class using this approach:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// Triplet class constructor function Triplet(first, second, third) { this.first = first; // first field of a Triplet this.second = second; // second field of a Triplet this.third = third; // third field of a Triplet } // Triplet class prototype functions Triplet.prototype = { // Returns the value at index i (0-based) getValue: function(i) { if (i === 0) return this.first; if (i === 1) return this.second; if (i === 2) return this.third; throw new Error("Invalid index"); }, // Returns a string representation of the Triplet toString: function() { // use parentheses and commas to separate the values return "(" + this.first + ", " + this.second + ", " + this.third + ")"; }, // Checks if two Triplets are equal equals: function(other) { if (!(other instanceof Triplet)) { return false; } // compare the values of each property return this.first === other.first && this.second === other.second && this.third === other.third; } }; // Example usage let t1 = new Triplet("Emma", 25, "F"); let t2 = new Triplet("Christopher", 30, "M"); let t3 = new Triplet("Emma", 25, "F"); console.log(t1.toString()); // (Emma, 25, F) console.log(t2); // {first: 'Christopher', second: 30, third: 'M'} console.log(t2.getValue(1)); // 30 console.log(t1.equals(t3)); // true console.log(t2.equals(t3)); // false |
That’s all about implementing a Triplet class in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)