Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LiuuY opened this issue Dec 29, 2016 · 1 comment
Closed

Comments

@LiuuY
Copy link

LiuuY commented Dec 29, 2016

// 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?

@timdorr
Copy link
Member

timdorr commented Dec 29, 2016

Well first off, you shouldn't be adding to primitives, as that affects the entire system and can have unexpected side effects.

We can probably swap out for fbjs's version instead, since it covers these edge cases and might be a bit faster too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants