JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

9.6.1 Example: A Set Class

9.6.1 Example: A Set Class


A set is a data structure that represents an unordered collection of values, with no duplicates. The fundamental operations on sets are adding values and testing whether a value is a member of the set, and sets are generally implemented so that these operations are fast. JavaScript’s objects are basically sets of property names, with values associated with each name. It is trivial, therefore, to use an object as a set of strings. Example 9-6 implements a more general Set class in JavaScript. It works by mapping any JavaScript value to a unique string, and then using that string as a property name. Objects and functions do not have a concise and reliably unique string representation, so the Set class must define an identifying property on any object or function stored in the set.

Example 9-6. Set.js: An arbitrary set of values

function Set() { // This is the constructor

this.values = {}; // The properties of this object hold the set

this.n = 0; // How many values are in the set

this.add.apply(this, arguments); // All arguments are values to add }

// Add each of the arguments to the set. Set.prototype.add = function() {

9.6 Object-Oriented Techniques in JavaScript | 215

for(var i = 0; i < arguments.length; i++) { // For each argument var val = arguments[i]; // The value to add to the set var str = Set._v2s(val); // Transform it to a string if (!this.values.hasOwnProperty(str)) { // If not already in the set

this.values[str] = val; // Map string to value this.n++; // Increase set size

} } return this; // Support chained method calls

};

// Remove each of the arguments from the set. Set.prototype.remove = function() {

for(var i = 0; i < arguments.length; i++) { // For each argument var str = Set._v2s(arguments[i]); // Map to a string if (this.values.hasOwnProperty(str)) { // If it is in the set

delete this.values[str]; // Delete it this.n--; // Decrease set size

} } return this; // For method chaining

};

// Return true if the set contains value; false otherwise. Set.prototype.contains = function(value) { return this.values.hasOwnProperty(Set._v2s(value)); };

// Return the size of the set. Set.prototype.size = function() { return this.n; };

// Call function f on the specified context for each element of the set. Set.prototype.foreach = function(f, context) { for(var s in this.values) // For each string in the set if (this.values.hasOwnProperty(s)) // Ignore inherited properties f.call(context, this.values[s]); // Call f on the value };

// This internal function maps any JavaScript value to a unique string. Set._v2s = function(val) {

switch(val) { case undefined: return 'u'; // Special primitive case null: return 'n'; // values get single-letter case true: return 't'; // codes. case false: return 'f'; default: switch(typeof val) {

case 'number': return '#' + val; // Numbers get # prefix. case 'string': return '"' + val; // Strings get " prefix. default: return '@' + objectId(val); // Objs and funcs get @

} }

// For any object, return a string. This function will return a different // string for different objects, and will always return the same string // if called multiple times for the same object. To do this it creates a // property on o. In ES5 the property would be nonenumerable and read-only.

function objectId(o) { var prop = "|**objectid**|"; // Private property name for storing ids if (!o.hasOwnProperty(prop)) // If the object has no id

o[prop] = Set._v2s.next++; // Assign it the next available return o[prop]; // Return the id

} }; Set._v2s.next = 100; // Start assigning object ids at this value.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31380.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1