Skip to main content

Bug Tracker

Side navigation

#12191 closed bug (fixed)

Opened August 03, 2012 07:24PM UTC

Closed November 24, 2012 10:23PM UTC

jQuery.type() should return "error" for native ECMAScript Error objects

Reported by: sime.vidas@gmail.com Owned by: rwaldron
Priority: low Milestone: 1.9
Component: core Version: 1.8rc1
Keywords: Cc:
Blocked by: Blocking:
Description

ECMAScript defines 12 different [[Class]] values - see [section 8.6.2](http://ecma-international.org/ecma-262/5.1/#sec-8.6.2). However, $.type() only "recognizes" 8 of those 12 values. The 4 that it doesn't recognize are:

  • "Arguments"
  • "Error"
  • "Math"
  • "JSON"

I understand why jQuery ignores Math, JSON, or the arguments object - their "type" can easily be comprehended based on their name.

What I don't understand is why $.type refuses to recognize Error objects.

$.type( new Error ) // "object", but should be "error"

Error objects are one of the native ECMAScript types of objects. $.type should be able to recognize them. To enable this recognition, all you have to do is add "Error" to the space-separated list here:

// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
    class2type[ "[object " + name + "]" ] = name.toLowerCase();
});

Like so:

// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
    class2type[ "[object " + name + "]" ] = name.toLowerCase();
});

The [[Class]] value of all instances of any of the native Error constructors (e.g. Error, SyntaxError, etc.) is "Error", so that this small change in the jQuery source code should make $.type recognize all native Error objects.

Btw, I have created a corresponding thread on Stack Oveflow - see here: http://stackoverflow.com/q/11797707/425275.

Attachments (0)
Change History (7)

Changed August 03, 2012 07:31PM UTC by rwaldron comment:1

status: newopen

Agree. Can you add them, along with supporting tests and make a pull request?

Changed August 03, 2012 07:31PM UTC by rwaldron comment:2

component: unfiledcore
milestone: None1.8
priority: undecidedlow
version: 1.7.21.8rc1

Changed August 03, 2012 07:35PM UTC by sime.vidas@gmail.com comment:3

Uh, I'm sorry, I don't know how to do that.

Changed August 03, 2012 07:56PM UTC by rwaldron comment:4

owner: → rwaldron
status: openassigned

No worries, I'll grab this and get it patched.

Changed August 10, 2012 05:53PM UTC by dmethvin comment:5

milestone: 1.81.8.1

Changed August 27, 2012 01:50PM UTC by dmethvin comment:6

milestone: 1.8.11.9

Changed November 24, 2012 10:23PM UTC by Dave Methvin comment:7

resolution: → fixed
status: assignedclosed

Fix #12191. jQuery.type should return "error" for Error objects.

Changeset: 67df705bf5205fa075ba6ceee2d7fd4763894a16