0

I can explain the question at its best by code:

            /**
             * javascript version of java.util.Set
             */
            Set: function(propertyKey) {
                var s = {}, hasValidPropsKey;
                propertyKey = propertyKey || 'id';

                /**
                 * Adds individual element or object or Array or Set
                 * @param ele
                 */
                this.add = function(ele) {
                    var tat = this;

                    /**
                     * Adds an element to the set.
                     * @param element can be primitive, object, array or Set
                     */
                    var addFunc = function(element) {
                        if(!element instanceof Object) {
                            s[element] = element; //yes its just json key value pair that we can leverage as set
                        } else {
                            s[element[propertyKey]] = element; //Key will alway be whatever initialized
                        }
                    };
                    if(ele instanceof Array) {
                        $.each(ele, function(i, val) {
                            addFunc(val);
                        });
                    } else if(ele instanceof 'Set'){//right here , how to do this
                        tat.add(ele.toArray());
                    } else {
                        addFunc(ele);
                    }
                };
//some more code...

Now see this piece of code

if(ele instanceof 'Set'){//right here , how to do this
  tat.add(ele.toArray());
}

How can I just refer the class/function or make a check that object is of same type?

Edit I got one way while drafting this question

if(ele.constructor.name === 'Set'){
    tat.add(ele.toArray());
}
old-monk
  • 799
  • 8
  • 20
  • 2
    `ele instanceof your.object.Set` Not sure why you're using a string for `Set`. –  Feb 23 '16 at 00:32
  • If you can't have a reference to the object, then just give the function a name: `Set: function Set(propertyKey) {...`, and just use `ele instanceof Set` –  Feb 23 '16 at 00:33

0 Answers0