Skip to content

shallowEqual always return false if add properties to Object.prototype #590

@LiuuY

Description

@LiuuY
// copy from src/utils/shallowEqual.js
const hasOwn = Object.prototype.hasOwnProperty

function shallowEqual(a, b) {
  if (a === b) return true

  let countA = 0
  let countB = 0
  
  for (let key in a) {
    if (hasOwn.call(a, key) && a[key] !== b[key]) return false
    countA++  // should check hasOwn.call(a, key) === true and then countA++ ?
  }

  for (let key in b) {
    if (hasOwn.call(b, key)) countB++
  }

  return countA === countB
}

// my example
Object.prototype.bar = 'bar'

let a = {x: '1'}
let b = {x: '1'}

shallowEqual(a, b) // return false

it's a misunderstanding or there are some reasons for this?

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