🌐 AI搜索 & 代理 主页
Skip to content

Commit 1ea092a

Browse files
jbedardtimmywil
authored andcommitted
Core: deprecate jQuery.type
Fixes gh-3605 Close gh-3895
1 parent 91fb181 commit 1ea092a

File tree

12 files changed

+85
-87
lines changed

12 files changed

+85
-87
lines changed

src/callbacks.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
define( [
22
"./core",
3+
"./core/toType",
34
"./var/isFunction",
45
"./var/rnothtmlwhite"
5-
], function( jQuery, isFunction, rnothtmlwhite ) {
6+
], function( jQuery, toType, isFunction, rnothtmlwhite ) {
67

78
"use strict";
89

@@ -130,7 +131,7 @@ jQuery.Callbacks = function( options ) {
130131
if ( !options.unique || !self.has( arg ) ) {
131132
list.push( arg );
132133
}
133-
} else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) {
134+
} else if ( arg && arg.length && toType( arg ) !== "string" ) {
134135

135136
// Inspect recursively
136137
add( arg );

src/core.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ define( [
1818
"./var/support",
1919
"./var/isFunction",
2020
"./var/isWindow",
21-
"./core/DOMEval"
21+
"./core/DOMEval",
22+
"./core/toType"
2223
], function( arr, document, getProto, slice, concat, push, indexOf,
2324
class2type, toString, hasOwn, fnToString, ObjectFunctionString,
24-
support, isFunction, isWindow, DOMEval ) {
25+
support, isFunction, isWindow, DOMEval, toType ) {
2526

2627
"use strict";
2728

@@ -237,17 +238,6 @@ jQuery.extend( {
237238
return true;
238239
},
239240

240-
type: function( obj ) {
241-
if ( obj == null ) {
242-
return obj + "";
243-
}
244-
245-
// Support: Android <=2.3 only (functionish RegExp)
246-
return typeof obj === "object" || typeof obj === "function" ?
247-
class2type[ toString.call( obj ) ] || "object" :
248-
typeof obj;
249-
},
250-
251241
// Evaluates a script in a global context
252242
globalEval: function( code ) {
253243
DOMEval( code );
@@ -395,7 +385,7 @@ function isArrayLike( obj ) {
395385
// hasOwn isn't used here due to false negatives
396386
// regarding Nodelist length in IE
397387
var length = !!obj && "length" in obj && obj.length,
398-
type = jQuery.type( obj );
388+
type = toType( obj );
399389

400390
if ( isFunction( obj ) || isWindow( obj ) ) {
401391
return false;

src/core/access.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
define( [
22
"../core",
3+
"../core/toType",
34
"../var/isFunction"
4-
], function( jQuery, isFunction ) {
5+
], function( jQuery, toType, isFunction ) {
56

67
"use strict";
78

@@ -13,7 +14,7 @@ var access = function( elems, fn, key, value, chainable, emptyGet, raw ) {
1314
bulk = key == null;
1415

1516
// Sets many values
16-
if ( jQuery.type( key ) === "object" ) {
17+
if ( toType( key ) === "object" ) {
1718
chainable = true;
1819
for ( i in key ) {
1920
access( elems, fn, i, key[ i ], true, emptyGet, raw );

src/core/toType.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
define( [
2+
"../var/class2type",
3+
"../var/toString"
4+
], function( class2type, toString ) {
5+
6+
"use strict";
7+
8+
function toType( obj ) {
9+
if ( obj == null ) {
10+
return obj + "";
11+
}
12+
13+
// Support: Android <=2.3 only (functionish RegExp)
14+
return typeof obj === "object" || typeof obj === "function" ?
15+
class2type[ toString.call( obj ) ] || "object" :
16+
typeof obj;
17+
}
18+
19+
return toType;
20+
} );

src/deprecated.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ define( [
22
"./core",
33
"./core/nodeName",
44
"./core/camelCase",
5+
"./core/toType",
56
"./var/isFunction",
67
"./var/isWindow",
78
"./var/slice",
89

910
"./event/alias"
10-
], function( jQuery, nodeName, camelCase, isFunction, isWindow, slice ) {
11+
], function( jQuery, nodeName, camelCase, toType, isFunction, isWindow, slice ) {
1112

1213
"use strict";
1314

@@ -76,6 +77,7 @@ jQuery.nodeName = nodeName;
7677
jQuery.isFunction = isFunction;
7778
jQuery.isWindow = isWindow;
7879
jQuery.camelCase = camelCase;
80+
jQuery.type = toType;
7981

8082
jQuery.now = Date.now;
8183

src/manipulation/buildFragment.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
define( [
22
"../core",
3+
"../core/toType",
34
"./var/rtagName",
45
"./var/rscriptType",
56
"./wrapMap",
67
"./getAll",
78
"./setGlobalEval"
8-
], function( jQuery, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) {
9+
], function( jQuery, toType, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) {
910

1011
"use strict";
1112

@@ -24,7 +25,7 @@ function buildFragment( elems, context, scripts, selection, ignored ) {
2425
if ( elem || elem === 0 ) {
2526

2627
// Add nodes directly
27-
if ( jQuery.type( elem ) === "object" ) {
28+
if ( toType( elem ) === "object" ) {
2829

2930
// Support: Android <=4.0 only, PhantomJS 1 only
3031
// push.apply(_, arraylike) throws on ancient WebKit

src/serialize.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
define( [
22
"./core",
3+
"./core/toType",
34
"./manipulation/var/rcheckableType",
45
"./var/isFunction",
56
"./core/init",
67
"./traversing", // filter
78
"./attributes/prop"
8-
], function( jQuery, rcheckableType, isFunction ) {
9+
], function( jQuery, toType, rcheckableType, isFunction ) {
910

1011
"use strict";
1112

@@ -39,7 +40,7 @@ function buildParams( prefix, obj, traditional, add ) {
3940
}
4041
} );
4142

42-
} else if ( !traditional && jQuery.type( obj ) === "object" ) {
43+
} else if ( !traditional && toType( obj ) === "object" ) {
4344

4445
// Serialize object item.
4546
for ( name in obj ) {

test/unit/ajax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ QUnit.module( "ajax", {
937937
dataType: "jsonp",
938938
crossDomain: crossDomain,
939939
success: function( data ) {
940-
assert.strictEqual( jQuery.type( data ), "array", "JSON results returned (GET, REST-like with param)" );
940+
assert.ok( Array.isArray( data ), "JSON results returned (GET, REST-like with param)" );
941941
}
942942
}
943943
];

test/unit/basic.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,13 @@ QUnit.test( "show/hide", function( assert ) {
7676
}
7777

7878
QUnit.test( "core", function( assert ) {
79-
assert.expect( 21 );
79+
assert.expect( 17 );
8080

8181
var elem = jQuery( "<div></div><span></span>" );
8282

8383
assert.strictEqual( elem.length, 2, "Correct number of elements" );
8484
assert.strictEqual( jQuery.trim( " hello " ), "hello", "jQuery.trim" );
8585

86-
assert.strictEqual( jQuery.type( null ), "null", "jQuery.type(null)" );
87-
assert.strictEqual( jQuery.type( undefined ), "undefined", "jQuery.type(undefined)" );
88-
assert.strictEqual( jQuery.type( "a" ), "string", "jQuery.type(String)" );
89-
9086
assert.ok( jQuery.isPlainObject( { "a": 2 } ), "jQuery.isPlainObject(object)" );
9187
assert.ok( !jQuery.isPlainObject( "foo" ), "jQuery.isPlainObject(String)" );
9288

test/unit/core.js

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -238,58 +238,6 @@ QUnit.test( "trim", function( assert ) {
238238
assert.equal( jQuery.trim( "\uFEFF \xA0! | \uFEFF" ), "! |", "leading/trailing should be trimmed" );
239239
} );
240240

241-
QUnit.test( "type", function( assert ) {
242-
assert.expect( 28 );
243-
244-
assert.equal( jQuery.type( null ), "null", "null" );
245-
assert.equal( jQuery.type( undefined ), "undefined", "undefined" );
246-
assert.equal( jQuery.type( true ), "boolean", "Boolean" );
247-
assert.equal( jQuery.type( false ), "boolean", "Boolean" );
248-
assert.equal( jQuery.type( Boolean( true ) ), "boolean", "Boolean" );
249-
assert.equal( jQuery.type( 0 ), "number", "Number" );
250-
assert.equal( jQuery.type( 1 ), "number", "Number" );
251-
assert.equal( jQuery.type( Number( 1 ) ), "number", "Number" );
252-
assert.equal( jQuery.type( "" ), "string", "String" );
253-
assert.equal( jQuery.type( "a" ), "string", "String" );
254-
assert.equal( jQuery.type( String( "a" ) ), "string", "String" );
255-
assert.equal( jQuery.type( {} ), "object", "Object" );
256-
assert.equal( jQuery.type( /foo/ ), "regexp", "RegExp" );
257-
assert.equal( jQuery.type( new RegExp( "asdf" ) ), "regexp", "RegExp" );
258-
assert.equal( jQuery.type( [ 1 ] ), "array", "Array" );
259-
assert.equal( jQuery.type( new Date() ), "date", "Date" );
260-
assert.equal( jQuery.type( new Function( "return;" ) ), "function", "Function" );
261-
assert.equal( jQuery.type( function() {} ), "function", "Function" );
262-
assert.equal( jQuery.type( new Error() ), "error", "Error" );
263-
assert.equal( jQuery.type( window ), "object", "Window" );
264-
assert.equal( jQuery.type( document ), "object", "Document" );
265-
assert.equal( jQuery.type( document.body ), "object", "Element" );
266-
assert.equal( jQuery.type( document.createTextNode( "foo" ) ), "object", "TextNode" );
267-
assert.equal( jQuery.type( document.getElementsByTagName( "*" ) ), "object", "NodeList" );
268-
269-
// Avoid Lint complaints
270-
var MyString = String,
271-
MyNumber = Number,
272-
MyBoolean = Boolean,
273-
MyObject = Object;
274-
assert.equal( jQuery.type( new MyBoolean( true ) ), "boolean", "Boolean" );
275-
assert.equal( jQuery.type( new MyNumber( 1 ) ), "number", "Number" );
276-
assert.equal( jQuery.type( new MyString( "a" ) ), "string", "String" );
277-
assert.equal( jQuery.type( new MyObject() ), "object", "Object" );
278-
} );
279-
280-
QUnit.test( "type for `Symbol`", function( assert ) {
281-
// Prevent reference errors
282-
if ( typeof Symbol !== "function" ) {
283-
assert.expect( 0 );
284-
return;
285-
}
286-
287-
assert.expect( 2 );
288-
289-
assert.equal( jQuery.type( Symbol() ), "symbol", "Symbol" );
290-
assert.equal( jQuery.type( Object( Symbol() ) ), "symbol", "Symbol" );
291-
} );
292-
293241
QUnit.asyncTest( "isPlainObject", function( assert ) {
294242

295243
assert.expect( 23 );
@@ -1328,7 +1276,7 @@ QUnit.test( "jQuery.parseHTML", function( assert ) {
13281276

13291277
nodes = jQuery.parseHTML( jQuery( "body" )[ 0 ].innerHTML );
13301278
assert.ok( nodes.length > 4, "Parse a large html string" );
1331-
assert.equal( jQuery.type( nodes ), "array", "parseHTML returns an array rather than a nodelist" );
1279+
assert.ok( Array.isArray( nodes ), "parseHTML returns an array rather than a nodelist" );
13321280

13331281
html = "<script>undefined()</script>";
13341282
assert.equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" );

0 commit comments

Comments
 (0)