The collection class through which JavaScript is implemented

Source: Internet
Author: User
Tags hasownproperty

A collection is a data structure that represents an unordered collection of distinct values. The underlying methods of a collection include adding values, detecting whether the values are in the collection, and this collection requires a common implementation to ensure operational efficiency.

A JavaScript object is a basic collection of property names and their corresponding values. A generic set class is implemented with JavaScript, which implements a mapping from JS to a unique string, and then uses the string as the property name. Objects and functions do not have such a concise and reliable unique string representation.

Therefore, the collection class must define a unique property identifier for each object or function in the collection.

1 //Generic collection Classes2 functionSet () {//This is a constructor function3      This. value={};//collection data is stored in the properties of the object4      This. n=0;//the number of values in the collection5      This. add.apply ( This, arguments);//Add all parameters to this collection6 }7 8 //add each parameter to the collection9Set.prototype.add=function(){Ten      for(vari=0;i<arguments.length;i++) {//traverse each parameter One         varVal=arguments[i];//values to add to the collection A         varStr=set._v2s (Val);//convert it to a string -         if(! This. Value.hasownproperty (str)) {//if not in the collection -              This. Value[str]=val;//the string and the value correspond. the              This. n++;//count of values in the collection -         } -     } -     return  This;//support for chained method calls + } -  + //removes an element from the collection, and the delete element is specified by the parameter ASet.prototype.remove=function(){ at      for(vari=0;i<arguments.length;i++){ -         varStr=Set._v2s (Arguments[i]); -         if( This. Values.hasownproperty (str)) { -             Delete  This. Value[str]; -              This. n--; -         } in     } -     return  This; to } +  - //iterates through all the elements in the collection, calling F in the specified context theSet.prototype.foreach=function(f,context) { *      for(varSinch  This. Values) { $         if( This. Value.hasownproperty (s))//Ignore inherited propertiesPanax NotoginsengF.call (Context, This. Value[s]); -     } the } +  A //This is an intrinsic function that corresponds to any JavaScript value and a unique string . theset._v2s=function(val) { +     Switch(val) { -          CaseUndefinedreturn' U '; $          Case NULL:returnN; $          Case true:return' t '; -          Case false:return' F '; -         default:Switch(typeofval) { the              Case' Number ':return' # ' +Val; -              Case' String ':return' "' +Val;Wuyi             default:return' @ ' +ObjectId (val); the         } -     } Wu      -     //for any object, a string will be returned About     //for different objects, this function returns a different string $     //The same string is always returned for multiple calls to the same object -     //to do this, it creates an attribute for O, in ES5, which is not enumerable and is read-only -     functionobjectId (o) { -         varprop= "|**objectid**|";//private attribute for storing ID A         if(!o.hasownproperty (prop))//if the object does not have an ID +o[prop]=set._v2s.next++;//assign the next value to it the         returnO[prop];//return this ID -     } $ }; theset._v2s.next=100;//set Initial ID value
View Code

From the JavaScript authoritative guide 9.6.1

The collection class through which JavaScript is implemented

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.