JavaScript一個集合的運算類

來源:互聯網
上載者:User

標籤:儲存   多次   cas   string   建構函式   字串   switch   turn   eof   

輸出都在控制台中:

<script type="text/javascript">function  Set() {        //這是一個建構函式    this.values = {};    //集合資料儲存在對象的屬性裡    this.n      = 0;    //集合中值的個數    this.add.apply( this, arguments );    //把所有參數都添加到這個集合中}//將每個參數都填加到集合中Set.prototype.add = function(){    console.log("參數的長度為:"+arguments.length);    for( var i = 0; i<arguments.length ; i++ ){        //遍曆每個參數        var val = arguments[ i ];                    //待添加到集合中的值        //console.log(val);        var str = Set._v2s(val);                    //把它轉化為字串        if( !this.values.hasOwnProperty(str) ){        //如果不在集合中            this.values[str] = val;                 //將字串和值對應起來            this.n++;                                //集合中的計數加一        }    }    return this;                                    //支援鏈式方法調用};//從集合刪除元素,這些元素由參數指定Set.prototype.remove = function(){    for(var i = 0; i<arguments.length ; i++){        //遍曆每個參數        var str = Set._v2s(arguments[i]);            //將字串和值對應起來        if(this.values.hasOwnProperty(str)){        //如果它存在集合中            delete this.values[str];                //刪除它            this.n--;                                //集合中值的計數減一        }    }    return this;};//如果集合中包含這個值,則返回true;否則返回falseSet.prototype.contains = function(value){    return this.values.hasOwnProperty(Set._v2s(value));};//返回集合的大小Set.prototype.size = function(){    return this.n;};//遍曆集合中的所有元素,在指定的上下文中調用fSet.prototype.foreach = function(f,context){    for( var s in this.values){                    //遍曆集合中的所有字串        if(this.values.hasOwnProperty(s))        //忽略繼承的屬性            f.call(context,this.values[s]);        //調用f,傳入value    }};//這個一個內建函式,用以將任意js值和唯一的字串對應起來Set._v2s = function(val){    //console.log(typeof val);    switch(val){        case undefined:         return "u";         //特殊的原始值        case null:                 return "n";                 case true:                 return "t";         //特殊的原始值        case false:             return "f";         //特殊的原始值        default: switch(typeof val){            case "number":         return ‘#‘+val;        //數字帶有#首碼            case "string":         return ‘"‘+val;        //字串帶有"首碼            default :             return "@"+objectId(val); //objs and funcs get @        }    }    //對任意對象來說,都會返回一個字串    //正對不同的對象,這個函數會返回不同字串    //對於同一個對象的多次調用,總是返回相同的字串    //為了做到這一點,它給o建立了一個屬性,在ES5中,這個屬性是不可枚舉且是唯讀    function objectId(o){        var prop = "|**objectid|";                    //私人屬性,用以存放id        if(!o.hasOwnProperty(prop))                    //如果隊形沒有id            o[prop] = Set._v2s.next++;                 //將下一個值賦給它        return o[prop];                                //返回這個id    }};Set._v2s.next = 100;         //設定初始id的值//執行個體測試:var s = new Set(1,2,4,4,"aaaaa",5,6,{a:1,b:3},6,7);console.log(s.values);console.log(s.size());</script>

 

JavaScript一個集合的運算類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.