Created
June 12, 2018 03:19
-
-
Save nodlAndHodl/812bc5cd9744b7a585b76c21bb3e90bf to your computer and use it in GitHub Desktop.
Object Comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'); | |
class Book{ | |
constructor(title, author, year, publisher){ | |
this.title = title; | |
this.author = author; | |
this.year = year; | |
this.publisher = publisher; | |
} | |
} | |
let book = new Book('The Old Man and the Sea', 'Earnest Hemingway', 1952, "Scribner's Sons"); | |
let sameBook = new Book('The Old Man and the Sea', 'Earnest Hemingway', 1952, "Scribner's Sons"); | |
let cloneBook = book; | |
describe('Book', function() { | |
describe('object comparison', function() { | |
it('should return true for comparison', function() { | |
assert.equal(book !== sameBook, true, "These books are not the same as their reference is different"); | |
}); | |
it('should return true for json stringify comparison', function(){ | |
assert.equal(JSON.stringify(book)===JSON.stringify(sameBook), true, "These json string literals are not the same.") | |
}); | |
it('should return true for json object comparison', function(){ | |
assert.equal(cloneBook === book, true); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the mocha test framework to show the nature of object comparison. Same type of an object does not equate to the same reference. Only the last one is the actual comparison of the same object to object comparison.