Skip to content

Instantly share code, notes, and snippets.

@shoupn
Created Jun 12, 2018
Embed
What would you like to do?
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);
});
});
});
@shoupn
Copy link
Author

shoupn commented Jun 12, 2018

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