Bug Tracker

Opened 11 years ago

Closed 11 years ago

#12191 closed bug (fixed)

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

Reported by: sime.vidas@… Owned by: Rick Waldron
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.

Change History (7)

comment:1 Changed 11 years ago by Rick Waldron

Status: newopen

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

comment:2 Changed 11 years ago by Rick Waldron

Component: unfiledcore
Milestone: None1.8
Priority: undecidedlow
Version: 1.7.21.8rc1

comment:3 Changed 11 years ago by sime.vidas@…

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

comment:4 Changed 11 years ago by Rick Waldron

Owner: set to Rick Waldron
Status: openassigned

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

comment:5 Changed 11 years ago by dmethvin

Milestone: 1.81.8.1

comment:6 Changed 11 years ago by dmethvin

Milestone: 1.8.11.9

comment:7 Changed 11 years ago by Dave Methvin

Resolution: fixed
Status: assignedclosed

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

Changeset: 67df705bf5205fa075ba6ceee2d7fd4763894a16

Note: See TracTickets for help on using tickets.