Core: Support Symbol wrapper objects in jQuery.type

In ECMAScript 2015 (ES6), the native typeof operator returns "symbol"
for Symbol primitives. As it is possible to wrap symbols using the
Object constructor, symbols can be objects as well as any other
primitive type in JavaScript and should be determined by jQuery.type.

Cherry-picked from 8a734344f2
Closes gh-2627
This commit is contained in:
Christian Grete 2015-10-03 00:28:34 +02:00 committed by Oleg Gaidarenko
parent 905ab09afc
commit c7cf28681e
2 changed files with 14 additions and 1 deletions

View File

@ -498,7 +498,7 @@ if ( typeof Symbol === "function" ) {
/* jshint ignore: end */ /* jshint ignore: end */
// Populate the class2type map // Populate the class2type map
jQuery.each( "Boolean Number String Function Array Date RegExp Object Error".split( " " ), jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
function( i, name ) { function( i, name ) {
class2type[ "[object " + name + "]" ] = name.toLowerCase(); class2type[ "[object " + name + "]" ] = name.toLowerCase();
} ); } );

View File

@ -264,6 +264,19 @@ QUnit.test( "type", function( assert ) {
assert.equal( jQuery.type( new MyObject() ), "object", "Object" ); assert.equal( jQuery.type( new MyObject() ), "object", "Object" );
} ); } );
QUnit.test( "type for `Symbol`", function( assert ) {
// Prevent reference errors
if( typeof Symbol !== "function" ) {
assert.expect( 0 );
return
}
assert.expect( 2 );
assert.equal( jQuery.type( Symbol() ), "symbol", "Symbol" );
assert.equal( jQuery.type( Object( Symbol() ) ), "symbol", "Symbol" );
});
QUnit.asyncTest( "isPlainObject", function( assert ) { QUnit.asyncTest( "isPlainObject", function( assert ) {
assert.expect( 16 ); assert.expect( 16 );