Javascript noob - 创建一个 "set"类,代码不工作

标签 javascript class set

我有以下代码(它实际上是在创建一个 Set 类,或者更明确地说是在创建一个无序的值集合,没有重复项)。

不幸的是,它不起作用 ==> 在调试器中运行它,我看到以下行返回“未定义”: o[prop] = Set._v2s.next++;

我认为这是因为 o="o",所以我不能将这个 Set 类与字符串一起使用。

知道如何修改代码以便我可以将它与字符串一起使用(就像我附加的示例中那样)吗?

代码如下:

function Set(){ // the constructor
     this.values = {}; // an empty object that will keep all the set elements' 
                       // names as properties 
     this.n = 0; // #values in the set
     this.add.apply(this, arguments); // when initially build the set then add all     the   arguments of the constructor into the set
}



// Add each of the arguments of the constructor to the set
Set.prototype.add = function(){
    for (var i=0; i<arguments.length; i++){ // for each argument of the constructor
        var val = arguments[i];
        var str = Set._v2s(val); // transform the value to a string
        if (!this.values.hasOwnProperty(str)){ // If not already in the set
            this.values[str]=val; // Load the element in the set
            this.n++;   
            }
        }
    return this; // support chained method call
};



// 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]);
        if (this.values.hasOwnProperty(str)){ // If the element is in the set already
        delete this.values[str];
        this.n--;   // Delete it
        }
    }
    return this;
};



// Return true if the set contains a 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)
        if (this.values.hasOwnProperty(s)) // ignore inherited props
             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';
            case true: return 't';
            case false: return 'f';
            default: switch(typeof val){
                case 'number': return '#' + val;    // numbers get the # prefix
                case 'string': return '@' + objectId(val);
            }
        }
};      
    // for any object, return a string ( a unique one per object, and if applied repeatedly on the same object will return the same string. The key technique is to add a (nonenumerable and read-only in ES5) property to object o.
    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];
};  

Set._v2s.next = 100;    // start assigning objectids at this value.     

var my_set = new Set("o","pojo");
alert(my_set.size);

最佳答案

好吧,通过运行你的代码,我发现了一个小错误 alert(my_set.size); 在定义中, Set.prototype.size = function(){ 返回 this.n; }; 所以,尺寸是一种方法。所以你应该调用它作为返回正确结果的方法,例如 var result = my_set.size()

关于Javascript noob - 创建一个 "set"类,代码不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17317996/

相关文章:

javascript - 在多维数组上添加一个值

Java 如何创建唯一的对象名称?

javascript - 如何将对象传播到 JavaScript 中的类属性中

c++ - 带有 lambda 比较器的集合对的 vector

javascript - 使用 if/else 的响应式 javascript

javascript - 为什么我的 javascript 测验无法正确加载?

javascript - 在使用 javascript 模块模式变体时访问父对象

java - 迭代时修改整数集和字符串集

java - 为什么 set.contains 不使用重写的 equals() 方法?

javascript - 而当我使用单值时,它会抛出错误。 Uncaught TypeError : health. 映射不是一个函数