tag:blogger.com,1999:blog-6442308477719289110.post4565998302411570612..comments2023-05-13T07:27:41.543-07:00Comments on foo(): How to detect a JavaScript arrayAlejandro Beltranhttp://www.blogger.com/profile/13533114582122558035noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6442308477719289110.post-76606658521228940982011-08-01T11:31:50.228-07:002011-08-01T11:31:50.228-07:00What about the instanceof keyword? Works similarly...What about the instanceof keyword? Works similarly to typeof but tests the constructor rather than the variable's type:<br /><br />[] instanceof Array //true<br />{} instanceof Object //true<br />var bob = new Person();<br />bob instanceof Person //truePaul Sweeneyhttps://www.blogger.com/profile/05790372905874351835noreply@blogger.comtag:blogger.com,1999:blog-6442308477719289110.post-78926262188873171232011-07-23T12:02:46.737-07:002011-07-23T12:02:46.737-07:00Developing this idea further:
(function () {
ar...Developing this idea further:<br /><br />(function () {<br /> arguments.constructor.prototype.type = function (a) { <br /> var c = this.callee;<br /> return (a = c.toString ().match (/function\s+\w*\s*\((.*?)\)/)[1].split (/\s*,\s*/).indexOf (a)) < 0 ? '?' : <br /> a >= this.length ? '' : <br /> !(a = this[a]) && (a === null || a === undefined) ? '' + a :<br /> ((a = Object.prototype.toString.call (a)).match (/object (\w+)/) || ['', a])[1].toLowerCase ();<br /> };<br />}) ();<br /><br />This JavaScript will install new property function called type to the arguments prototype.<br /><br />Now you can call arguments.type ('a') which will return the type of the argument called a. The return values are the same as my argType function in my previous comment.<br /><br />These were tested on Chrome, Firefox and Opera running under Ubuntu, I'd be interested in knowing if they work on other browsers.Hans B Pufalhttps://www.blogger.com/profile/13538316069706512155noreply@blogger.comtag:blogger.com,1999:blog-6442308477719289110.post-73492110452424042812011-07-23T01:39:24.035-07:002011-07-23T01:39:24.035-07:00Your post inspired me to create the following two ...Your post inspired me to create the following two functions.<br /><br />The first, called type, returns 'number', string', null', undefined', 'array', 'regexp', 'boolean' etc according to the type of its argument.<br /><br />function type (v) {<br /> return !v && (v === null || v === undefined) ? '' + v :<br /> ((v = Object.prototype.toString.call (v)).match (/object (\w+)/) || ['', v])[1].toLowerCase ();<br />}<br /><br />The second, called argType, takes a single string argument which is the name of an argument of the calling function, and returns that arguments type.<br /><br />If the argType argument is not the name of in the callers argument list, it returns '?', if callers named argument was not specified in its invoking call, argType returns ''.<br /><br />function argType (a) { <br /> var c = argType.caller; <br /> a = c.toString ().match (/function \w*\((.*?)\)/)[1].split (',').indexOf (a);<br /> return a < 0 ? '?' : c.arguments.length > a ? type (c.arguments[a]) : '';<br />}<br /><br />Hope these are of interest.Hans B Pufalhttps://www.blogger.com/profile/13538316069706512155noreply@blogger.com