1. 程式人生 > >js 判斷兩個物件是否相同

js 判斷兩個物件是否相同

isObjectEqual (a, b) {
    // 獲取a、b的型別
    var classNameA = toString.call(a)
    var classNameB = toString.call(b)

    if (classNameA === '[object Object]' && classNameB === '[object Object]') {
      var propsA = Object.getOwnPropertyNames(a)
      var propsB = Object.getOwnPropertyNames(b)
      if
(propsA.length !== propsB.length) { return false } for (let i = 0; i < propsA.length; i++) { var propName = propsA[i] if (a[propName] !== b[propName]) { return false } } return true } }