JavaScript Implementation Collection

Source: Internet
Author: User

function set () {
This.datastore=[];
This.add=add;
This.remove.remove;
This.size=size;
This.union=union;
This.intersect=intersect;
This.subset=subset;
This.difference=difference;
This.show=show;

}

function Add (data) {
if (this.dataStore.indexOf (data) <0) {//The uniqueness of the collection
This.dataSource.push (data);
}
else{
return false;
}
}

function remove (data) {
var pos=this.datastore.indexof (data);
if (pos>-1)
{
This.dataStore.splice (pos,1);
}

Else
{
return false;
}
}


Function Show () {
return this.datastore;

}


Function Union (SET)
{
var tempset=new set;
for (Var i=0;i<this.datastore.length;i++)
{
Tempset.add (This.datastore[i]);
}

for (Var i=0;i<set.datastore.length;++i)
{
if (!tempset.contains (Set.datastore[i]))
{
Tempset.dataStore.push (Set.datastore[i]);

}
}

return tempset;

}


function intersect (SET)
{
var tempset=new set ();
for (Var i=0;i<this.datastore.length;i++)
{
if (Set.contains (This.datastore[i])) {
Tempset.add (Set.datastore[i]);

}
}

return tempset;
}


function subset (SET) {
if (This.size () >set.size ()) {
return false;
}

else{
for (var key in This.datastore)
{

if (!set.contains (key)) {
return false;
}

}

}

return true;

}


function size () {
return this.dataStore.length;

}


function difference (SET)
{

var tem,pset=new set ();

for (var key in This.datastore)
{
if (!set.contains (key))
{
Tempset.add (key);
}

}

return tempset;

}

JavaScript Implementation Collection

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.