IsPlainObject, chose?

Est-ce que ce fn:

function isplainobj ( obj ) { return Object.prototype.toString.call( obj ) === "[object Object]"; } 

Faire la même chose que jQuery's: $ .isPlainObject ()?

Non, ce n'est pas le cas.

Voici sa mise en œuvre:

 isPlainObject: function( obj ) { var key; // Must be an Object. // Because of IE, we also have to check the presence of the constructor property. // Make sure that DOM nodes and window objects don't pass through, as well if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { return false; } try { // Not own constructor property must be Object if ( obj.constructor && !core_hasOwn.call(obj, "constructor") && !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { return false; } } catch ( e ) { // IE8,9 Will throw exceptions on certain host objects #9897 return false; } // Support: IE<9 // Handle iteration over inherited properties before own properties. if ( jQuery.support.ownLast ) { for ( key in obj ) { return core_hasOwn.call( obj, key ); } } // 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 || core_hasOwn.call( obj, key ); }, 

Il vérifie s'il s'agit d'un objet type, s'il possède ou non un constructeur et si ses propriétés sont des propriétés propres.

Par exemple, avec votre fonction, une instance d'une classe renverra true mais dans ce cas, car elle possède un constructeur, elle renverra false.

Selon le code source jQuery ici http://james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.isPlainObject , j'ai traversé toutes les possibilités traitées dans jQuery's plain Object. La fonction que vous avez mentionnée est de passer tous les tests. Et les deux exécutent de la même manière. Veuillez vérifier les cases testées ci-dessous

Isplainobj ({});
vrai

Isplainobj (function () {});
faux

Isplainobj (document);
faux

Isplainobj (fenêtre);
faux

Isplainobj ($);
faux

Isplainobj ({});
vrai

Isplainobj (isplainobj);
faux

Isplainobj ({});
vrai

Selon ma compréhension, l'objet qui possède toutes ses propriétés est un objet simple. S'il vous plait corrigez moi si je me trompe.

Modifier:

Selon les lignes ci-dessous dans jQuery

 // Not own constructor property must be Object if (obj.constructor && !core_hasOwn.call(obj, "constructor") && !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { return false; } 

L'objet ne devrait pas avoir un constructor propriété et obj.constructor.prototype ne devrait pas avoir isPrototypeOf pour s'assurer qu'il ne se trouve pas dans une chaîne de prototypes. Consultez https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf pour plus d'informations.

Ce test simple devrait le faire

  obj!=null && typeof(obj)=="object" && Object.getPrototypeOf(obj)==Object.prototype 

// pour garantir Object.getPrototypeOf

  Object.getPrototypeOf||(Object.getPrototypeOf=function(obj){ return obj.__proto__ || obj.prototype || (obj.constructor&&obj.constructor.prototype) || Object.prototype }); 

De la source jQuery:

 isPlainObject: function( obj ) { var key; // Must be an Object. // Because of IE, we also have to check the presence of the constructor property. // Make sure that DOM nodes and window objects don't pass through, as well if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { return false; } try { // Not own constructor property must be Object if ( obj.constructor && !core_hasOwn.call(obj, "constructor") && !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { return false; } } catch ( e ) { // IE8,9 Will throw exceptions on certain host objects #9897 return false; } // Support: IE<9 // Handle iteration over inherited properties before own properties. if ( jQuery.support.ownLast ) { for ( key in obj ) { return core_hasOwn.call( obj, key ); } } // 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 || core_hasOwn.call( obj, key ); },