JavaScript data Structures-mimic similar set classes defined in ES6

Source: Internet
Author: User
Tags hasownproperty

1 functionSet () {2     varitems={};3 4      This. has=function(value) {5         returnItems.hasownproperty (value);6     };7 8      This. add=function(value) {9         if(! This. has (value)) {Tenitems[value]=value; One             return true; A         } -         return false; -     }; the  -      This. remove=function(value) { -         if( This. has (value)) { -             DeleteItems[value]; +             return true; -         } +         return false; A     }; at  -      This. clear=function(){ -items={}; -     }; -  -     //method One: ES5 above in /*this.size=function () { - return Object.keys (items). length; to     }*/ +  -     //method Two: Compatible with the new strong the     //hasOwnProperty () to exclude properties of the items prototype itself *      This. size=function(){ $         varCount=0;Panax Notoginseng          for(varPropinchitems) { -             if(Items.hasownproperty (prop)) { thecount++; +             } A         } the         returncount; +     }; -  $     //method One: ES5 above $     /*this.values=function () { - return Object.keys (items); -     };*/ the  -     //compatibility is relatively strongWuyi      This. values=function(){ the         varkeys=[]; -          for(varKeyinchitems) { Wu             if(Items.hasownproperty (key)) { - Keys.push (key); About             } $         } -         returnkeys; -     }; -  A  +     //collection of and operations the      This. union=function(otherset) { -         varUnionset =NewSet (); $         varvalues= This. values (); the          for(vari=0;i<values.length;i++){ the Unionset.add (Values[i]); the         } the  -values=otherset.values (); in          for(i;i<values.length;i++){ the Unionset.add (Values[i]); the         } About         returnUnionset; the     }; the  the     //intersection of sets +      This. intersection=function(otherset) { -         varIntersectionset =NewSet (); the         varvalues= This. values ();Bayi          for(vari=0;i<values.length;i++){ the             if(Otherset.has (Values[i])) { the Intersectionset.add (Values[i]); -             } -         } the         returnIntersectionset; the     }; the  the     //Difference Set Operation -      This. difference=function(otherset) { the         varDifferenceset =NewSet (); the         varvalues= This. values (); the          for(vari=0;i<values.length;i++){94             if(!Otherset.has (Values[i])) { the Differenceset.add (Values[i]); the             } the         }98         returnDifferenceset; About     }; - 101     //determine if it is a subset of Otherset102      This. subset=function(otherset) {103         if( This. Size () >otherset.size ()) {104             return false; the}Else{106             varvalues= This. values ();107              for(vari=0;i<values.length;i++){108                 if(!Otherset.has (Values[i])) {109                     return false; the                 }111             } the             return true;113         } the  the}

In this implementation, it is important to note that the set class is a set of mathematical concepts, and the elements in the collection must not be duplicated and unique. So using objects to initialize items, you can use the uniqueness of key-value pairs to achieve the nature of the set. The associated collection nature allows you to view knowledge of mathematical set concepts. The example above can be done by writing some simple HTML examples.

JavaScript data Structures-mimic similar set classes defined in ES6

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.