mirror of
https://github.com/jquery/jquery.git
synced 2026-01-31 22:48:09 -05:00
Core: Simplify isPlainObject
Fixes gh-2986 Close gh-2998
This commit is contained in:
committed by
Timmy Willison
parent
10fc59007d
commit
e0d3bfa770
36
src/core.js
36
src/core.js
@@ -1,6 +1,7 @@
|
||||
define( [
|
||||
"./var/arr",
|
||||
"./var/document",
|
||||
"./var/getProto",
|
||||
"./var/slice",
|
||||
"./var/concat",
|
||||
"./var/push",
|
||||
@@ -8,10 +9,13 @@ define( [
|
||||
"./var/class2type",
|
||||
"./var/toString",
|
||||
"./var/hasOwn",
|
||||
"./var/fnToString",
|
||||
"./var/ObjectFunctionString",
|
||||
"./var/support",
|
||||
"./core/DOMEval"
|
||||
], function( arr, document, slice, concat,
|
||||
push, indexOf, class2type, toString, hasOwn, support, DOMEval ) {
|
||||
], function( arr, document, getProto, slice, concat, push, indexOf,
|
||||
class2type, toString, hasOwn, fnToString, ObjectFunctionString,
|
||||
support, DOMEval ) {
|
||||
|
||||
var
|
||||
version = "@VERSION",
|
||||
@@ -225,28 +229,24 @@ jQuery.extend( {
|
||||
},
|
||||
|
||||
isPlainObject: function( obj ) {
|
||||
var key;
|
||||
var proto, Ctor;
|
||||
|
||||
// Not plain objects:
|
||||
// - Any object or value whose internal [[Class]] property is not "[object Object]"
|
||||
// - DOM nodes
|
||||
// - window
|
||||
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
|
||||
// Detect obvious negatives
|
||||
// Use toString instead of jQuery.type to catch host objects
|
||||
if ( !obj || toString.call( obj ) !== "[object Object]" ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Not own constructor property must be Object
|
||||
if ( obj.constructor &&
|
||||
!hasOwn.call( obj, "constructor" ) &&
|
||||
!hasOwn.call( obj.constructor.prototype || {}, "isPrototypeOf" ) ) {
|
||||
return false;
|
||||
proto = getProto( obj );
|
||||
|
||||
// Objects with no prototype (e.g., `Object.create( null )`) are plain
|
||||
if ( !proto ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Own properties are enumerated firstly, so to speed up,
|
||||
// if last one is own, then all properties are own
|
||||
for ( key in obj ) {}
|
||||
|
||||
return key === undefined || hasOwn.call( obj, key );
|
||||
// Objects with prototype are plain iff they were constructed by a global Object function
|
||||
Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
|
||||
return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
|
||||
},
|
||||
|
||||
isEmptyObject: function( obj ) {
|
||||
|
||||
5
src/var/ObjectFunctionString.js
Normal file
5
src/var/ObjectFunctionString.js
Normal file
@@ -0,0 +1,5 @@
|
||||
define( [
|
||||
"./fnToString"
|
||||
], function( fnToString ) {
|
||||
return fnToString.call( Object );
|
||||
} );
|
||||
5
src/var/fnToString.js
Normal file
5
src/var/fnToString.js
Normal file
@@ -0,0 +1,5 @@
|
||||
define( [
|
||||
"./hasOwn"
|
||||
], function( hasOwn ) {
|
||||
return hasOwn.toString;
|
||||
} );
|
||||
3
src/var/getProto.js
Normal file
3
src/var/getProto.js
Normal file
@@ -0,0 +1,3 @@
|
||||
define( function() {
|
||||
return Object.getPrototypeOf;
|
||||
} );
|
||||
Reference in New Issue
Block a user