
Considering objects in JavaScript
In JavaScript, objects are fundamental data structures that allow you to store and manipulate collections of key-value pairs. They provide a way to represent complex data and encapsulate related properties and functions.
Objects in JavaScript can be created using object literals, constructor functions, or the newer ES6 class syntax. Object literals provide a simple way to create objects by defining their properties and methods directly. Constructor functions and classes allow you to create object instances with shared behavior and properties.
Objects can have properties, which are key-value pairs that store data, and methods, which are functions associated with the object. You can access and modify object properties using dot notation (e.g., object.property) or bracket notation (e.g., object["property"]). Methods can be invoked by using parentheses after the method name (e.g., object.method()).
JavaScript follows a prototype-based inheritance model. Objects can have a prototype, which acts as a template for inheriting properties and methods. You can create object hierarchies and inherit properties and methods from other objects by setting the prototype using the prototype property or by using the extends keyword with classes.
You can add, modify, or delete properties of an object dynamically using assignment (object.property = value) or the delete keyword. You can also iterate over object properties using loops or built-in methods like Object.keys(), Object.values(), or Object.entries().
In JavaScript, objects are passed by reference. When you assign an object to a variable or pass it as a function argument, you're working with a reference to the object rather than a copy. Be aware of this behavior when modifying objects, as changes made to an object can affect other variables referencing the same object.
JavaScript supports object-oriented programming concepts. You can use objects, inheritance, and encapsulation to create modular and reusable code. Techniques like object composition and mixins can be employed to achieve code reuse and maintainable object-oriented designs.
Using strings to differentiate objects with name, id attributes in your code that are used to identify an item as the one you're looking for.
if (obj.label === "name") {
console.log(obj);
}
Your project eventually expands (in size, relevance, popularity, or all of these at once). There are more items to identify from one another, thus it requires more strings. For example, a change in your label naming practice rises as the strings become longer. Now you need to track down and replace every occurrence of those strings. As a result, the commit for that modification is considerably larger than it needs to be. Which improves your image in the eyes of the uninformed. It also makes your life unpleasant since it is now much more difficult to identify the root cause of regression in your git history.
const joe_black_person = "Joe Black";
const joe_black_company = "Joe Black";
// Do they have the same name? //
joe_black_person === joe_black_company; //true//
// Are they the same thing? //
joe_black_person === joe_black_company; //true//
Strings don't work well for identification. Your editor or IDE won't verify that the string is the one you intended; you must take uniqueness and mistakes into account.
What if I told you that I have a better solution? The one who gets rid of all the problems and adds more value can achieve more. You check the objects themselves to determine if they are what you are looking for.
// Do they have a same name? //
joe_black_person.name === joe_black_company.name; // true//
// Are they the same thing?//
joe_black_person === joe_black_company; //false//
But it’s just a part of the picture. I know we use objects all over the place, and it’s nothing new to group things in them. But I bet you don’t use them exactly like that. I rarely see two objects being compared like that because you never know what’s in there or where it came from. Objects are created and changed all the time. It is more likely for them to be compared by the values of their properties than the objects themselves. And the reason for that is that objects aren’t suitable for that kind of use. They are too capable. To allow that use case and many others, we have to, on the one hand, reduce some capabilities of objects and, on the other, implement some more. And in the end, we’ll get what I call Primitive Objects.
I want to cover some aspects of JavaScript that help bring objects closer to primitive values, which in return would allow us to benefit from common language features that aren’t usually associated with an object, like comparisons and arithmetic operators. In the following part, we’ll look closely into practical examples and tools to work with such objects.
Understanding how objects work in JavaScript is essential for building complex applications, organizing code, and implementing object-oriented patterns. Objects provide a flexible and powerful way to represent and manipulate data, encapsulate functionality, and build modular software.
Commnets (0)