未加星标

JavaScript Object Comparison Essentials

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
Comparison Essentials

You can compare strings like this "String1" === "String1" and this would be true , however, the moment you assign a value to as a variable it becomes a different reference in memory and thus becomes a different reference and the comparison breaks down. You may have seen an example of a string comparison function looking like the below function. Takes in parameters two string and compares them, returning a value and letting you know if they are indeed the same, that is why the .toString() function is being called on each instance of a string. As in our first example of comparing the string literals, it returns true .

function stringCompare(string1, string2) { if (string1.toString() < string2.toString()) return -1; if (string1.toString() > string2.toString()) return 1; return 0; }

This holds true for object comparison in javascript. If you are new development there a couple of things you need to keep in mind when doing a comparison of objects. I’ve got a small example below of comparing objects of the below Book class. These are some small unit tests basically showing you that even though the title, author, date of publication and publisher are the same they are referencing two different objects. It helps to use a real-world example and think if this as comparing two books, that came of the print press, one after the other.

Equality Operators

Mind your comparison operators, and almost always use the strict comparison like this === for equality and !== for non-equality. Using == and != can get you into trouble (non-strict), and I almost always default to using strict except in a few rare occasions. This is one of the different things about JavaScript as opposed to many other languages for equality comparison. So be careful, as == can be used for comparing string values to integer or floating number values in some instances, and care should be taken.

Unit Test Code

I’m using node and mocha to run the below unit tests as examples in the code below. If you want to run them yourself you can you’ll need mocha framework for testing. If you have node installed you can install mocha by following the npm instructions here . I love mocha as it’s a nice dependable way for running JavaScript tests, which I’ve used in some pretty large production applications and it’s a solid testing framework. Look in the code comments for reasons and logic below. Run your mocha tests by using mocha book.js or whatever your file name is.

/*jshint esversion: 6 */ let assert = require('assert'; class Book{ constructor(title, author, year, publisher){ this.title = title; this.author = author; this.year = year; this.publisher = publisher; } } //one of my all-time favorite American classics 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"); //assign another variable to the same in-memory reference object let cloneBook = book; describe('Book', function() { describe('object comparison', function() { it('should return true for comparison', function() { //We are saying that these books are not the same object, even though they have the same content. 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(){ //this was similar to our previous string comparison example assert.equal(JSON.stringify(book)===JSON.stringify(sameBook), true, "These json string literals are not the same."); }); it('should return true for object comparison as they are referencing same object', function(){ assert.equal(cloneBook === book, true); }); }); }); If you run the tests you should see something like this:
JavaScript Object Comparison Essentials
As always I hope you found this useful, please subscribe for updates and share this if you found it useful. Please leave a comment below, and questions if you have them. Happy coding!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

tags: comparison,true,function,same
分页:12
转载请注明
本文标题:JavaScript Object Comparison Essentials
本站链接:https://www.codesec.net/view/604928.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(13)