Page 1 of 1

check for HTML* type on object

Posted: Mon Feb 15, 2010 4:31 pm
by daedalus__
hey i am running to a wall here trying to figure out an objects type. i made a little addChildren function which is great but what if i accidently invoke it on an object that is not an HTMLElement?

oh uhhhh.. this seems kind of silly but heres what i did:

Code: Select all

 
Object.prototype.appendChildren = function () 
    {
        if (arguments.length == 0)
            throw "appendChildren() requires at least one parameter!";
        
        // the magic
        if (!(/HTML/.test(this)))
            throw "appendChildren must be called from an HTML* object.";
        
        for (var i = 0; i < arguments.length; i++)
            this.appendChild(arguments[i]);
    }
 

Re: check for HTML* type on object

Posted: Mon Feb 15, 2010 6:04 pm
by Darhazer
typeof ?

You can perform

Code: Select all

typeof this.appendChild != 'undefined'
to check if method exists :-)

Re: check for HTML* type on object

Posted: Mon Feb 15, 2010 6:21 pm
by daedalus__
lol crap. thats a lot better idea :p