Bug Tracker

Ticket #12191 (closed bug: fixed)

Opened 2 years ago

Last modified 20 months ago

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

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

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

comment:1 Changed 2 years ago by rwaldron

  • Status changed from new to open

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

comment:2 Changed 2 years ago by rwaldron

  • Priority changed from undecided to low
  • Version changed from 1.7.2 to 1.8rc1
  • Component changed from unfiled to core
  • Milestone changed from None to 1.8

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

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

comment:4 Changed 2 years ago by rwaldron

  • Owner set to rwaldron
  • Status changed from open to assigned

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

comment:5 Changed 2 years ago by dmethvin

  • Milestone changed from 1.8 to 1.8.1

comment:6 Changed 2 years ago by dmethvin

  • Milestone changed from 1.8.1 to 1.9

comment:7 Changed 20 months ago by Dave Methvin

  • Status changed from assigned to closed
  • Resolution set to fixed

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

Changeset: 67df705bf5205fa075ba6ceee2d7fd4763894a16

Note: See TracTickets for help on using tickets.