Skip to content

Instantly share code, notes, and snippets.

@nodlAndHodl
Created June 12, 2018 03:19
Show Gist options
  • Save nodlAndHodl/812bc5cd9744b7a585b76c21bb3e90bf to your computer and use it in GitHub Desktop.
Save nodlAndHodl/812bc5cd9744b7a585b76c21bb3e90bf to your computer and use it in GitHub Desktop.
Object Comparison
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);
});
});
});
@nodlAndHodl
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment