Typeof array == object??

Author Topic: Typeof array == object??  (Read 1256 times)

Offline ngomaichiTopic starter

  • Trade Count: (0)
  • Newbie
  • *
  • Thank You 0
  • Posts: 5
  • Karma: 0
    • car racing games download for mobile
Typeof array == object??
« on: 05-25-2016, 04:04:17 »
dev in Firefox v37.0.2 on Max OSX Yosemite

var a = new Array('first', 'second', 'third', 'fourth')
alert(typeof(a)); // => 'object' (?)

I am developing a module that needs to distinguish the input argument type.
So I am using:
Code:
//constructor function code... etc...
var arrayInput = []
var objectInput = {}
switch(typeof(arg)) // arg == ['first', 'second', 'third', 'fouth', ]
   {
    case 'array': // for example ['first', 'second', 'third', 'fouth', ]
    alert('case array')
    arrayInput = arg;
    break;
    case 'string':
    break;
    case 'object': // for example {'find':'literal', 'type':'string', 'proc':function(a){/* callback code */}}
    alert('case object')
    objectInput = arg;
    break;
   }
// ... etc ...
array literal is coming up as 'object'
So I tried the code at the top of this post: specify an array using new Array, still an object type.
What is going on here, beside maybe problems with my code I haven't noticed yet?

Edit: It appears that the only way to distinguish an array from an object in this context is to look for
the length attribute (as the texts I use tell me, everything in javascript is an object, except primitives.
Yet strings have a length attribute and have a constructor function, and behave similar to arrays in the
sense that each char has an index). I won't complain.

Thanks for time and attention


Offline ngomaichiTopic starter

  • Trade Count: (0)
  • Newbie
  • *
  • Thank You 0
  • Posts: 5
  • Karma: 0
    • car racing games download for mobile
Re: Typeof array == object??
« Reply #1 on: 05-30-2016, 21:09:23 »
Can you help me???
--------------

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
1252 Views
Last post 08-19-2011, 01:56:39
by Walalayo
0 Replies
1052 Views
Last post 07-25-2013, 10:25:11
by Patrecia
0 Replies
661 Views
Last post 12-21-2015, 02:27:50
by chinmay.sahoo
1 Replies
733 Views
Last post 04-20-2016, 03:31:08
by TomClarke
0 Replies
672 Views
Last post 08-11-2016, 07:22:02
by beingchinmay