jsは2つのオブジェクトの属性と値が一致するかどうかを判断します.

966 ワード

//                    true/false
    isObjectValueEqual (a, b) {   
      //   a b    
      var aProps = Object.getOwnPropertyNames(a);
      var bProps = Object.getOwnPropertyNames(b);
      //      length    
      if (aProps.length != bProps.length) {
          return false;
      }
      //       ,          
      for (var i = 0; i < aProps.length; i++) {
        var propName = aProps[i];
        if (a[propName] !== b[propName]) {
            return false;
        }
      }
      return true;
    },
var obj1 = { name: "Benjamin", sex : "male"};

var obj2 = { name: "Benjamin", sex : "male"};

var obj3 = obj1;//obj1 ob3               

console.log(isObjectValueEqual(obj1, obj2));//true
 
console.log(obj1 == obj3);//Outputs: true
 
console.log(obj1 === obj3);//Outputs: true
 
console.log(obj2 == obj3);//Outputs: false
 
console.log(obj2 === obj3);//Outputs: false