How to determine equality for two JavaScript objects? -


a strict equality operator tell if 2 object types equal. however, there way tell if 2 objects equal, much hash code value in java?

stack overflow question is there kind of hashcode function in javascript? similar question, requires more academic answer. scenario above demonstrates why necessary have one, , i'm wondering if there equivalent solution.

the short answer

the simple answer is: no, there no generic means determine object equal in sense mean. exception when strictly thinking of object being typeless.

the long answer

the concept of equals method compares 2 different instances of object indicate whether equal @ value level. however, specific type define how equals method should implemented. iterative comparison of attributes have primitive values may not enough, there may attributes not considered part of object value. example,

 function myclass(a, b)  {      var c;      this.getclazy = function() {          if (c === undefined) c = * b // imagine * expensive          return c;      }   } 

in above case, c not important determine whether 2 instances of myclass equal, a , b important. in cases c might vary between instances , yet not significant during comparison.

note issue applies when members may instances of type , these each required have means of determining equality.

further complicating things in javascript distinction between data , method blurred.

an object may reference method called event handler, , not considered part of 'value state'. whereas object may assigned function performs important calculation , thereby makes instance different others because references different function.

what object has 1 of existing prototype methods overridden function? still considered equal instance otherwise identical? question can answered in each specific case each type.

as stated earlier, exception strictly typeless object. in case sensible choice iterative , recursive comparison of each member. 1 has ask 'value' of function?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -