Skip to content

Instantly share code, notes, and snippets.

@tcdevs
Forked from nicbell/1_primitive_comparison.js
Created January 5, 2017 08:27

Revisions

  1. @nicbell nicbell revised this gist Oct 3, 2013. 3 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion 1_primitive_comparison.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    <!--Primitive Type Comparison-->
    //Primitive Type Comparison
    var a = 1;
    var b = 1;
    var c = a;
    2 changes: 1 addition & 1 deletion 2_obeject_comparison.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    <!--Object comparison-->
    //Object comparison
    var a = { blah: 1 };
    var b = { blah: 1 };
    var c = a;
    2 changes: 1 addition & 1 deletion object.compare.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    <!--How To Compare Object Values-->
    //How To Compare Object Values
    var a = { blah: 1 };
    var b = { blah: 1 };
    var c = a;
  2. @nicbell nicbell revised this gist Oct 3, 2013. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions 2_obeject_comparison.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <!--Object comparison-->
    var a = { blah: 1 };
    var b = { blah: 1 };
    var c = a;
    1 change: 1 addition & 0 deletions object.compare.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <!--How To Compare Object Values-->
    var a = { blah: 1 };
    var b = { blah: 1 };
    var c = a;
  3. @nicbell nicbell revised this gist Oct 3, 2013. 3 changed files with 29 additions and 3 deletions.
    9 changes: 9 additions & 0 deletions 1_primitive_comparison.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <!--Primitive Type Comparison-->
    var a = 1;
    var b = 1;
    var c = a;

    console.log(a == b); //true
    console.log(a === b); //true
    console.log(a == c); //true
    console.log(a === c); //true
    8 changes: 8 additions & 0 deletions 2_obeject_comparison.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    var a = { blah: 1 };
    var b = { blah: 1 };
    var c = a;

    console.log(a == b); //false
    console.log(a === b); //false
    console.log(a == c); //true
    console.log(a === c); //true
    15 changes: 12 additions & 3 deletions object.compare.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,14 @@
    var a = { blah: 1 };
    var b = { blah: 1 };
    var c = a;
    var d = { blah: 2 };

    Object.compare = function (obj1, obj2) {
    //Loop through properties in object 1
    for (var p in obj1) {
    //Check property exists on both objects
    if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;

    switch (typeof (obj1[p])) {
    //Deep compare objects
    case 'object':
    @@ -18,10 +23,14 @@ Object.compare = function (obj1, obj2) {
    if (obj1[p] != obj2[p]) return false;
    }
    }

    //Check object 2 for any extra properties
    for (var p in obj2) {
    if (typeof (obj1[p]) == 'undefined') return false;
    }
    return true;
    };
    };

    console.log(Object.compare(a, b)); //true
    console.log(Object.compare(a, c)); //true
    console.log(Object.compare(a, d)); //false
  4. @nicbell nicbell revised this gist Oct 3, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion a
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    c
  5. @nicbell nicbell revised this gist Oct 3, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions a
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    c
  6. @nicbell nicbell revised this gist Jul 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion object.compare.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ Object.compare = function (obj1, obj2) {
    break;
    //Compare function code
    case 'function':
    if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;;
    if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;
    break;
    //Compare values
    default:
  7. @nicbell nicbell created this gist Jul 25, 2013.
    27 changes: 27 additions & 0 deletions object.compare.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    Object.compare = function (obj1, obj2) {
    //Loop through properties in object 1
    for (var p in obj1) {
    //Check property exists on both objects
    if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;

    switch (typeof (obj1[p])) {
    //Deep compare objects
    case 'object':
    if (!Object.compare(obj1[p], obj2[p])) return false;
    break;
    //Compare function code
    case 'function':
    if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;;
    break;
    //Compare values
    default:
    if (obj1[p] != obj2[p]) return false;
    }
    }

    //Check object 2 for any extra properties
    for (var p in obj2) {
    if (typeof (obj1[p]) == 'undefined') return false;
    }
    return true;
    };