Skip to content

Document jQuery.isEmptyObject doesn't take symbols into account #1192

Open
@XiaoY0324

Description

@XiaoY0324

Description

On version 3.6.0, I found a vulnerability,I find that the method of detecting empty objects is not perfect.

isEmptyObject: function( obj ) {
  var name;

  for ( name in obj ) {
     return false;
   }
   return true;
},

The downside of writing this is that the properties of the prototype chain and the Symbol type are not taken into account,I have a better way to implement it as follows.

isEmptyObject: function( obj ) {
    var keys = Object.keys(obj);

    // 看浏览器是否支持 Symbol
    if (typeof Symbol !== "undefined") {
       keys = keys.concat(Object.getOwnPropertySymbols(obj));
    }
  
    return keys.length === 0;
}

Link to test case

This direct modification of the source code test is good, I do not have a service to run this test. I'm looking forward to hearing from you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions