
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Creating a chained operation class in JavaScript
Problem
We are supposed to create a user defined data type Streak in JavaScript that can be chained to any extent with value and operations alternatively
The value can be one of the following strings −
→ one, two three, four, five, six, seven, eight, nine
The operation can be one of the following strings −
→ plus, minus
For example, if we implement the following in context of our class −
Streak.one.plus.five.minus.three;
Then the output should be −
const output = 3;
Output Explanation
Because the operations that took place are −
1 + 5 - 3 = 3
Example
Following is the code −
const Streak = function() { let value = 0; const operators = { 'plus': (a, b) => a + b, 'minus': (a, b) => a - b }; const numbers = [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ]; Object.keys(operators).forEach((operator) => { const operatorFunction = operators[operator]; const operatorObject = {}; numbers.forEach((num, index) => { Object.defineProperty(operatorObject, num, { get: () => value = operatorFunction(value, index) }); }); Number.prototype[operator] = operatorObject; }); numbers.forEach((num, index) => { Object.defineProperty(this, num, { get: () => { value = index; return Number(index); } }); }); }; const streak = new Streak(); console.log(streak.one.plus.five.minus.three);
Output
Following is the console output −
3
- Related Articles
- Creating a Projectile class to calculate height horizontal distance and landing in JavaScript
- Chained Exceptions in C#
- Creating a Stack in Javascript
- Creating a Queue in Javascript
- Creating a Graph in Javascript
- Chained exception in Java\n
- Creating POJO Class for Kotlin
- Inverse operation in JavaScript
- What are chained exceptions in Java?
- Creating a Set using Javascript
- Creating a BinaryTree using Javascript
- Creating a Priority Queue using Javascript
- Creating a linked list using Javascript
- Creating a hash table using Javascript
- JavaScript - Creating a Custom Image Slider

Advertisements