
- 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
The Set Class in Javascript
Here is the complete implementation of the MySet class.
Example
class MySet { constructor() { this.container = {}; } display() { console.log(this.container); } has(val) { return this.container.hasOwnProperty(val); } add(val) { if (!this.has(val)) { this.container[val] = val; return true; } return false; } delete(val) { if (this.has(val)) { delete this.container[val]; return true; } return false; } clear() { this.container = {}; } forEach(callback) { for (let prop in this.container) { callback(prop); } } static union(s1, s2) { if (!s1 instanceof MySet || !s2 instanceof MySet) { console.log("The given objects are not of type MySet"); return null; } let newSet = new MySet(); s1.forEach(elem => newSet.add(elem)); s2.forEach(elem => newSet.add(elem)); return newSet; } static difference(s1, s2) { if (!s1 instanceof MySet || !s2 instanceof MySet) { console.log("The given objects are not of type MySet"); return null; } let newSet = new MySet(); s1.forEach(elem => newSet.add(elem)); s2.forEach(elem => newSet.delete(elem)); return newSet; } }
- Related Articles
- Set value in the JavaTuples KeyValue class
- Set key in the JavaTuples KeyValue class
- The Stack Class in Javascript
- The Queue Class in Javascript
- The PriorityQueue Class in Javascript
- The Dictionary Class in Javascript
- The HashTable Class in Javascript
- The set() method of AbstractList class in Java
- The Linked List Class in Javascript
- The set() method of Java AbstractSequentialList class
- Class Keyword in JavaScript
- The Doubly Linked List class in Javascript
- AVL Tree class in Javascript
- Complete Graph Class in Javascript
- First class function in JavaScript

Advertisements