You'll have to excuse i'm fairly new to Javascript, so i might be
missing the blatantly obvious :)
I thought you could just call isArray(['1','2','3']), but this throws
an error for me in Firefox 2.1

Looking into it I wasn't sure why prototype had certain type testing
functions (isString, isArray...) extended onto the Object Object (Line
110 onwards).
I kinda thought maybe they should be on the Object.prototype but guess
this would just make every instanced object heavy.

So i put this together :-
(function(){

        function typeOf(Obj){
                var str = typeof Obj;
                if (str === 'object') {
                        if(Obj){
                                if (Obj instanceof Array) str = 'array';
                        }else str = 'null';
                }
                return str;
        }

        this.isArray = function(Obj){ return typeOf(Obj) === 'array'; },

        this.isObject = function(Obj){ return typeOf(Obj) === 'object'; },

        this.isNull = function(Obj){ return typeOf(Obj) === 'null'; },

        this.isFunction = function(Obj){ return typeof Obj === 'function'; },

        this.isUndefined = function(Obj){ return typeof Obj ===
'undefined'; },

        this.isString = function(Obj){ return typeof Obj === 'string'; },

        this.isNumber = function(Obj){ return typeof Obj === 'number'; },

        this.isNumeric = function(Obj){ return typeof Obj != 'boolean' &&
Obj !== null && !isNaN(+ Obj); },

        this.isBoolean = function(Obj){ return typeof Obj === 'boolean' }

        this.isString = function(Obj){ return typeof Obj === 'string'; }

})();

I originally tried Object.extend(__proto__,{ ...... etc but then
noticed IE and Opera doesn't support it
I tried window instead of __proto__ and this worked but wasn't sure if
it would be good practice so went for the above method instead.
This puts the functions into the global scope whilst keeping them
encapsulated and tidy so i can call isArray(['1','2','3']) etc... with
no problem.

Just after some feedback really, any reason not to do it like this? or
any glaringly obvious bad practices??
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to