SORU
31 EKİM 2011, PAZARTESİ


JavaScript taklit ayarlar?

JavaScript çalışıyorum. Listesini saklamak istiyorumbenzersizdize değerleri sırasız, aşağıdaki özelliklere sahip:

  1. sormak için hızlı bir şekilde 'listesinde mi?
  2. yapmak için hızlı bir şekilde 'sil eğer listede varsa listeden Bir'
  3. yapmak için hızlı bir şekilde 'eğer zaten mevcut değilse listesine ekle".

Benim asıl istediğim bir dizi. JavaScript bir dizi taklit etmek için en iyi yolu için herhangi bir öneriniz var mı?

Anahtarlar özellikleri depolama question recommends using an Object, ve tüm değerler true bu mantıklı bir yol var mı?

CEVAP
31 EKİM 2011, PAZARTESİ


Bir Nesne kullanarak çok iyi çalışıyor. obj nesne ve A sette faaliyet göstermek istediğiniz değeri içeren bir değişken ise, o zaman bu yapabilirsiniz:

Başlatma kodu:

// create empty object
var obj = {};

// or create an object with some items already in it
var obj = {"1":true, "2":true, "3":true, "9":true};

Soru 1:Listede A:

if (A in obj) {
    // put code here
}

Soru 2:'Bir' eğer varsa listeden . sil

delete obj[A];

Soru 3:'Bir' eğer orada zaten öyle olmasa listesine ekleyin

obj[A] = true;

Bütünlüğü için, A listesinde olup olmadığını test için bu biraz daha güvenli

if (Object.prototype.hasOwnProperty.call(obj, A))
    // put code here
}

üste-yerleşik yöntem ve/veya özellikler arasında potansiyel çatışma nedeniyle constructor gibi özellikleri olan bir Nesne.


Kenar Çubuğu:ECMA 6 geçerli çalışma sürümü (henüz tamamlanmamış) bir yerleşik Kümesi nesnesi vardır. Chrome, Firefox ve uygulandığı, ancak deneysel özellikleri etkinleştirmek için Chrome özel bir seçenek gerektirir. Yerleşik Set nesne de olabilir bu yüzden yapıyor gibi bir dize için tüm anahtarları zorlamak değil, 5 avantajı ve "5" ayrı anahtarlar gibi. Burada yetenekleri bazı açıklar ve MDN's documentation sette nesne an article.

Şimdi Eğer tarayıcı destekliyorsa, şimdi ve otomatik olarak dahili feragat edecek kümesi nesnesi kullanarak başlayabilirsin yani ES6 kümesi nesnesi için bir dolgu yazdım. Bu şekilde IE 7 için geri çalışacak ES6 uyumlu kod yazıyorsun avantajı vardır. Ancak, bazı dezavantajları da vardır. ES6 set arayüzü for (item of mySet) gibi şeyler yapabilirsiniz ES6, kullanımına yararlanır ve sizin için otomatik olarak ayarlamak yineleme olacaktır. Ancak, dil bu tür dolgu ile uygulanması da mümkün bulunmaktadır. Hala yeni ES6 dil özelliklerini kullanmadan ES6 bir dizi yineleme yapabilirsiniz, ama açıkçası yeni dil özellikleri olmadan diğeri de aşağıda eklediğim arayüzü ayarlamak kadar kolay değil.

Senin için en iyi iki baktıktan sonra çalışır karar verebilirsiniz. Dolgu işte ES6 kümesi: https://github.com/jfriend00/ES6-Set.

BİLGİNİZE, benim kendi test, Firefox v29 Set uygulaması tam spec mevcut taslak üzerinde güncel olmadığını fark ettim. Örneğin, spec açıklar gibi .add() yöntem çağrıları zinciri edemezsin ve benim dolgu destekler. Bu muhtemelen henüz tamamlanmamış gibi hareket belirtimi meselesidir.


Önceden oluşturulmuş nesneler Kümesi:Eğer zaten yerleşik nesne bu yöntem için çalışan bir set kullanabileceğiniz herhangi bir tarayıcı kullanabilirsiniz bir dizi farklı önceden tasarlanmış nesneleri uygulamak farklı ayarlar. Bir dizi temel nesne uygulayan küçük kodu olan bir miniSet var. Ayrıca daha fazla özellik zengin set nesne ve çeşitli bilgiler içeren bir Sözlük (hadi saklamak/almak bir değer için her tuş) ve bir ObjectSet (hadi devam nesneler kümesi ya da JS nesneleri veya DOM nesneleri nerede ya tedarik işlev oluşturur bir benzersiz anahtar için her bir veya ObjectSet oluşturur anahtarı için).

İşte miniSet için bir kopyasını kod (en güncel kod here on github).

"use strict";
//-------------------------------------------
// Simple implementation of a Set in javascript
//
// Supports any element type that can uniquely be identified
//    with its string conversion (e.g. toString() operator).
// This includes strings, numbers, dates, etc...
// It does not include objects or arrays though
//    one could implement a toString() operator
//    on an object that would uniquely identify
//    the object.
// 
// Uses a javascript object to hold the Set
//
// This is a subset of the Set object designed to be smaller and faster, but
// not as extensible.  This implementation should not be mixed with the Set object
// as in don't pass a miniSet to a Set constructor or vice versa.  Both can exist and be
// used separately in the same project, though if you want the features of the other
// sets, then you should probably just include them and not include miniSet as it's
// really designed for someone who just wants the smallest amount of code to get
// a Set interface.
//
// s.add(key)                      // adds a key to the Set (if it doesn't already exist)
// s.add(key1, key2, key3)         // adds multiple keys
// s.add([key1, key2, key3])       // adds multiple keys
// s.add(otherSet)                 // adds another Set to this Set
// s.add(arrayLikeObject)          // adds anything that a subclass returns true on _isPseudoArray()
// s.remove(key)                   // removes a key from the Set
// s.remove(["a", "b"]);           // removes all keys in the passed in array
// s.remove("a", "b", ["first", "second"]);   // removes all keys specified
// s.has(key)                      // returns true/false if key exists in the Set
// s.isEmpty()                     // returns true/false for whether Set is empty
// s.keys()                        // returns an array of keys in the Set
// s.clear()                       // clears all data from the Set
// s.each(fn)                      // iterate over all items in the Set (return this for method chaining)
//
// All methods return the object for use in chaining except when the point
// of the method is to return a specific value (such as .keys() or .isEmpty())
//-------------------------------------------


// polyfill for Array.isArray
if(!Array.isArray) {
    Array.isArray = function (vArg) {
        return Object.prototype.toString.call(vArg) === "[object Array]";
    };
}

function MiniSet(initialData) {
    // Usage:
    // new MiniSet()
    // new MiniSet(1,2,3,4,5)
    // new MiniSet(["1", "2", "3", "4", "5"])
    // new MiniSet(otherSet)
    // new MiniSet(otherSet1, otherSet2, ...)
    this.data = {};
    this.add.apply(this, arguments);
}

MiniSet.prototype = {
    // usage:
    // add(key)
    // add([key1, key2, key3])
    // add(otherSet)
    // add(key1, [key2, key3, key4], otherSet)
    // add supports the EXACT same arguments as the constructor
    add: function() {
        var key;
        for (var i = 0; i < arguments.length; i  ) {
            key = arguments[i];
            if (Array.isArray(key)) {
                for (var j = 0; j < key.length; j  ) {
                    this.data[key[j]] = key[j];
                }
            } else if (key instanceof MiniSet) {
                var self = this;
                key.each(function(val, key) {
                    self.data[key] = val;
                });
            } else {
                // just a key, so add it
                this.data[key] = key;
            }
        }
        return this;
    },
    // private: to remove a single item
    // does not have all the argument flexibility that remove does
    _removeItem: function(key) {
        delete this.data[key];
    },
    // usage:
    // remove(key)
    // remove(key1, key2, key3)
    // remove([key1, key2, key3])
    remove: function(key) {
        // can be one or more args
        // each arg can be a string key or an array of string keys
        var item;
        for (var j = 0; j < arguments.length; j  ) {
            item = arguments[j];
            if (Array.isArray(item)) {
                // must be an array of keys
                for (var i = 0; i < item.length; i  ) {
                    this._removeItem(item[i]);
                }
            } else {
                this._removeItem(item);
            }
        }
        return this;
    },
    // returns true/false on whether the key exists
    has: function(key) {
        return Object.prototype.hasOwnProperty.call(this.data, key);
    },
    // tells you if the Set is empty or not
    isEmpty: function() {
        for (var key in this.data) {
            if (this.has(key)) {
                return false;
            }
        }
        return true;
    },
    // returns an array of all keys in the Set
    // returns the original key (not the string converted form)
    keys: function() {
        var results = [];
        this.each(function(data) {
            results.push(data);
        });
        return results;
    },
    // clears the Set
    clear: function() {
        this.data = {}; 
        return this;
    },
    // iterate over all elements in the Set until callback returns false
    // myCallback(key) is the callback form
    // If the callback returns false, then the iteration is stopped
    // returns the Set to allow method chaining
    each: function(fn) {
        this.eachReturn(fn);
        return this;
    },
    // iterate all elements until callback returns false
    // myCallback(key) is the callback form
    // returns false if iteration was stopped
    // returns true if iteration completed
    eachReturn: function(fn) {
        for (var key in this.data) {
            if (this.has(key)) {
                if (fn.call(this, this.data[key], key) === false) {
                    return false;
                }
            }
        }
        return true;
    }
};

MiniSet.prototype.constructor = MiniSet;

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AutoStream's Garage419

    AutoStream's

    15 EKİM 2007
  • International Secret Agents Concerts

    Internationa

    2 Aralık 2008
  • SRI International

    SRI Internat

    30 NİSAN 2008