<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Barrios Freddy</title>
    <description>The latest articles on Forem by Barrios Freddy (@barriosdfreddy).</description>
    <link>https://forem.com/barriosdfreddy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55666%2F105305c3-fac9-44ff-9f48-14eeaa47c0e7.jpg</url>
      <title>Forem: Barrios Freddy</title>
      <link>https://forem.com/barriosdfreddy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/barriosdfreddy"/>
    <language>en</language>
    <item>
      <title>Domain Driven Design.</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Wed, 24 May 2023 14:23:50 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/domain-driven-design-4ph7</link>
      <guid>https://forem.com/barriosdfreddy/domain-driven-design-4ph7</guid>
      <description>&lt;p&gt;Domain-Driven Design(DDD) is a collection of principles and patterns that help developers craft elegant object systems. Properly applied it can lead to software abstractions called domain models. These models encapsulate complex business logic, closing the gap between business reality and code.&lt;/p&gt;

&lt;p&gt;These domain experts use company- or industry-standard terminology. In DDD, this vocabulary is called the Ubiquitous Language&lt;/p&gt;

&lt;p&gt;I would expect to find corresponding class names in the code.&lt;/p&gt;

&lt;p&gt;Bounded Contexts guide you toward thinking of software as a portfolio of models. &lt;br&gt;
Modules help you organize a larger single model into smaller chunks. &lt;br&gt;
Aggregate roots as a technique for organizing small collaborations between a few highly related classes.&lt;/p&gt;

&lt;p&gt;In most enterprise systems there are course-grained areas of responsibility. DDD calls this top level of organization a Bounded Context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bounded Context to Context Map&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A context map encapsulates multiples bounded context&lt;/p&gt;

&lt;p&gt;One very important thought about Bounded Contexts: each context owns its own Ubiquitous Language. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anti-Corruption Layers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An Anti-Corruption Layer (ACL) is another DDD pattern that encourages you to create gatekeepers that work to prevent non-domain concepts from leaking into your model. They keep the model clean.&lt;/p&gt;

&lt;p&gt;"At their heart, repositories are actually a type of ACL. They keep SQL or object-relational mapping (ORM) constructs outside of your model." &lt;/p&gt;

&lt;p&gt;A System of Single Responsibilities&lt;/p&gt;

&lt;p&gt;I've mentioned that DDD provides a pattern language for structuring rich domain models. By implementing these patterns, you get a certain level of adherence to the SRP for free, and that's certainly valuable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Entities Have an Identity and a Life&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An entity is a "thing" in your system. It's often helpful to think about these in terms of nouns: people, places, and, well, things.&lt;/p&gt;

&lt;p&gt;"Think of entities as units of behavior rather than as units of data. Try to put your logic in the entities that own them."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Value Objects Describe Things&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Value objects are descriptors or properties important in the domain you are modeling.&lt;/p&gt;

&lt;p&gt;Value objects are immutable. They are incapable of change once they are created.&lt;/p&gt;

&lt;p&gt;"Part of the beauty of value objects is that they describe the properties of entities in a much more elegant and intention-revealing way"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregate Roots Combine Entities&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An aggregate root is a special kind of entity that consumers refer to directly. Identifying aggregate roots allows you to avoid over-coupling the objects that comprise your model by imposing a few simple rules. You should note that aggregate roots guard their sub-entities zealously.&lt;/p&gt;

&lt;p&gt;The biggest rule to keep in mind is that aggregate roots are the only kind of entity to which your software may hold a reference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Domain Services Model Primary Operations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you have operations or processes that do not have an identity or lifecycle in your domain. Domain services give you a tool for modeling these concepts. They're typically stateless and highly cohesive, often providing a single public method and sometimes an overload for acting on sets.&lt;/p&gt;

&lt;p&gt;"When there are a number of dependencies involved in a behavior and I can't find a natural place on an entity to place that behavior, I'll use a service."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repositories Save and Dispense Aggregate Roots&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Repositories represent an in-memory collection, and the conventional wisdom is that you end up with one repository per aggregate root.&lt;/p&gt;

&lt;p&gt;Repositories are a good candidate for a super class or what Martin Fowler refers to as the Layer Supertype pattern.&lt;/p&gt;

&lt;p&gt;Strategic Design&lt;br&gt;
  Describe objects&lt;/p&gt;

&lt;p&gt;Core domain&lt;/p&gt;

&lt;p&gt;Entities&lt;/p&gt;

&lt;p&gt;Tactical Design&lt;br&gt;
  "Entities are mutable"&lt;br&gt;
  Values objects = unmutables&lt;/p&gt;

&lt;p&gt;Aggregate&lt;/p&gt;

&lt;p&gt;Repositories(DB) and services(Logic)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/best-practice-an-introduction-to-domain-driven-design" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/best-practice-an-introduction-to-domain-driven-design&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>MongoDB replication: What you need to know</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Wed, 12 May 2021 14:34:20 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/mongodb-replication-what-you-need-to-know-2299</link>
      <guid>https://forem.com/barriosdfreddy/mongodb-replication-what-you-need-to-know-2299</guid>
      <description>&lt;p&gt;Database replication is the process of copying data from a database in one server to a database in another one. This protects your information in case of a disaster or hardware failure and improves the reliability and accessibility of the information. Replication provides high availability and redundancy, crucial topics when you manage a huge amount of data.&lt;/p&gt;

&lt;p&gt;In MongoDB, this process is done through a replica set which in simple words, it's a group of mongod processes to keep the same data across different servers.  A replica set must have three nodes at least. One of them must be the primary and the rest secondary ones. A replication structure can have up to 50 nodes and 7 voting members maximum, more nodes might decrease the performance. But, I think you would not need this quantity of nodes unless you have a global requirement. That is another story.&lt;/p&gt;

&lt;p&gt;There are three kinds of MongoDB nodes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Primary&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The primary is the member in the replica set that receives write and read operations. But, read operations can be pointed out to secondary nodes changing the configuration at the moment to perform the query. Besides the replica set can have only one primary node at most. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Secondary&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The secondary is the node where the data is replicated to maintain a copy. A replica set can have one or more secondary nodes. The clients can not write data to secondary ones, only they can read from them. In certain cases, a secondary node might become a primary node. I’ll talk a little more below. The secondary nodes replicate the primary's oplog(operations log) and apply the operations to their data sets asynchronously. Since the replication is asynchronous, the function of the database can continue if one or more of the members fail.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Arbiter&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The arbiter node does not have a copy of the data set and can not become a primary. However, an arbiter participates in elections for primary.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevdqu0rx7tkdzv04sdyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevdqu0rx7tkdzv04sdyk.png" alt="replicaset1" width="605" height="364"&gt;&lt;/a&gt;&lt;/p&gt;
Image 1. Replica set



&lt;p&gt;If the primary node goes down, one of the secondary nodes will become the primary, this through an election, until the original primary node goes online back again. Also, if both secondary nodes are unavailable, the primary node would become secondary and the database will be unreachable. To keep in mind, the primary node receives all write operations and the majority of the read operations. Therefore, the replica set must have a primary node active.&lt;/p&gt;

&lt;h3&gt;
  
  
  Member election
&lt;/h3&gt;

&lt;p&gt;An election is used to determine which member will become primary. An election is triggered to respond to a variety of events, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initiating a replicate set&lt;/li&gt;
&lt;li&gt;Adding a new node to the replica set&lt;/li&gt;
&lt;li&gt;Performing maintenance tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The average time before a cluster elects a new primary should not typically exceed 12 seconds. The election algorithm will attempt to have the secondary with the highest priority available. On the other hand, members with a priority value of 0 can not become primary and do not seek election.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ehaaaxtsun7l9jy6mpu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ehaaaxtsun7l9jy6mpu.png" alt="replicaset2" width="626" height="354"&gt;&lt;/a&gt;&lt;/p&gt;
Image 2. A secondary node becomes primary



&lt;p&gt;For durability, the write operations have a mechanism to replicate the data in a certain number of nodes and give feedback to the client. This mechanism is called, "Write concern" which consists of a number of data-bearing members that must acknowledge a Write concern before the operation returns as successful. By default, the replica sets have a value of 1 as write concern, therefore only the primary must acknowledge the write before returning write concern acknowledgment. Also, you can increase the number of members required to acknowledge the write operation, as many you want but if the number is high, it increases the latency as the client must wait until it receives the acknowledgment from the number of members specified. On the other hand, you can set a write concern of "majority" which calculates more than half of members to receive its acknowledgment.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is write concern?
&lt;/h3&gt;

&lt;p&gt;Write concern describes the level of acknowledgment requested from MongoDB for write operations to a replica set. The specified number must be lesser than the number of member nodes. Also, you can set the write concern as “majority” what does it’s to request acknowledgment that the write operations have propagated to the calculated majority of the members. It means, in a cluster of three data-bearing nodes the acknowledgment will be sent to the client after two of them receive the data. By default the primary and one of the secondaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is read preference?
&lt;/h3&gt;

&lt;p&gt;For the read operations, you can specify a read preference that describes how the database routes the query to members of the replica set. By default, the primary node receives the read operation but the clients can specify a read preference to send the read operations to secondary nodes. The following are the options for the read preference.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;primary&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All read operations come from the primary node&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;primaryPreferred&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Most read operations come from the primary node but if this is unavailable the data would come from the secondary nodes&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;secondary&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All read operations come from the secondary nodes&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;secondaryPreferred&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Most read operations come from the secondary nodes but if none of these are available the data comes from the primary node&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;nearest&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The result of reading operations may come from any of the members of the replica set, it doesn't matter if this is primary or secondary.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To sum up, the process of replicating data across different servers is a good option for high availability and durability. In simple words, replication is the process of creating redundant data to safeguard it. In addition, MongoDB brings this concept to its core, therefore it already has the tools to use it easily.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to empty an array in JavaScript</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Fri, 11 Dec 2020 03:17:55 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/how-to-empty-an-array-in-javascript-1io1</link>
      <guid>https://forem.com/barriosdfreddy/how-to-empty-an-array-in-javascript-1io1</guid>
      <description>&lt;p&gt;The simple way to empty an array in JavaScript is through assigning &lt;strong&gt;0&lt;/strong&gt; to the property &lt;strong&gt;length&lt;/strong&gt; of the array affected. For Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1,3,5,7,9];

numbers.length = 0;

console.log(numbers);
// []

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is possible because the &lt;strong&gt;length&lt;/strong&gt; property is read/write property in an array. Therefore, you can assign zero and the array will be clear. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
      <category>tricks</category>
    </item>
    <item>
      <title>Array methods: findIndex</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Thu, 10 Dec 2020 02:40:07 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/array-methods-findindex-8ke</link>
      <guid>https://forem.com/barriosdfreddy/array-methods-findindex-8ke</guid>
      <description>&lt;p&gt;The &lt;strong&gt;findIndex&lt;/strong&gt; receives a function (predicate) that accepts three arguments and returns a value that is coercible to the Boolean value true or false.&lt;/p&gt;

&lt;p&gt;The predicate returns true. If such an element is found, findIndex immediately returns the index of the first element value. Otherwise, findIndex returns -1, indicating that no element passed the test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [
    {name: 'Freddy', age: 10}, 
    {name: 'Francisco', age: 3}, 
    {name: 'Emilia', age: 4}
];

const preschoolerIndex = array.findIndex(kid =&amp;gt; kid.age &amp;lt; 5);

console.log(preschoolerIndex);
// 1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Parameters
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;callback&lt;/strong&gt;&lt;br&gt;
Testing function to execute on each value in the array, taking 3 arguments:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;element&lt;/strong&gt;&lt;br&gt;
The current element in the array.&lt;br&gt;
&lt;strong&gt;index&lt;/strong&gt; (Optional)&lt;br&gt;
The index (position) of the current element in the array.&lt;br&gt;
&lt;strong&gt;array&lt;/strong&gt; (Optional)&lt;br&gt;
The array that &lt;em&gt;find&lt;/em&gt; was called on.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;thisArg&lt;/strong&gt; (Optional)&lt;br&gt;
Object to use as this inside callback.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The findIndex function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Array methods: find</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Fri, 04 Dec 2020 04:35:03 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/array-methods-find-26am</link>
      <guid>https://forem.com/barriosdfreddy/array-methods-find-26am</guid>
      <description>&lt;p&gt;The &lt;strong&gt;find&lt;/strong&gt; method receives a callback function that is used as a testing function and, optionally, an object to use as &lt;em&gt;this&lt;/em&gt; inside. This returns the value of the first element in the provided array that satisfies the testing. Otherwise, the &lt;strong&gt;find&lt;/strong&gt; method returns &lt;em&gt;undefined&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [
 {name: 'Emilia', age: 4},
 {name: 'Freddy', age: 10}
];

const preschooler = array.find(kid =&amp;gt; kid.age &amp;lt; 5);

console.log(preschooler);
// { name: 'Emilia', age: 4 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Parameters
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;callback&lt;/strong&gt;&lt;br&gt;
Testing function to execute on each value in the array, taking 3 arguments:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;element&lt;/strong&gt;&lt;br&gt;
The current element in the array.&lt;br&gt;
&lt;strong&gt;index&lt;/strong&gt; (Optional)&lt;br&gt;
The index (position) of the current element in the array.&lt;br&gt;
&lt;strong&gt;array&lt;/strong&gt; (Optional)&lt;br&gt;
The array that &lt;em&gt;find&lt;/em&gt; was called on.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;thisArg&lt;/strong&gt; (Optional)&lt;br&gt;
Object to use as this inside callback.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;find&lt;/strong&gt; is a method very useful in order to encounter a specific element into an array, providing a function that will be invoked on each element until finding the value that satisfies the testing. Therefore, be careful, the usage of this method in some cases can be inefficient.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Asynchronous code: Promises</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Wed, 02 Dec 2020 04:22:34 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/asynchronous-code-promises-p3a</link>
      <guid>https://forem.com/barriosdfreddy/asynchronous-code-promises-p3a</guid>
      <description>&lt;p&gt;A promise is an object representing the eventual completion or failure of an asynchronous operation.&lt;/p&gt;

&lt;p&gt;Basically, a promise is an object where you can attach a numberless callback function which can be pass as arguments to the chainable methods &lt;strong&gt;then&lt;/strong&gt;, &lt;strong&gt;catch&lt;/strong&gt; and &lt;strong&gt;finally&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Promise constructor receives the executor function which waits for two functions, in the first position the &lt;code&gt;resolve&lt;/code&gt; function which will be executed if everything is done, and in the second position the &lt;code&gt;reject&lt;/code&gt; function that will be executed in case of an error, for example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new Promise(function(resolve, reject) {
    const name = "Freddy"
    resolve(name)
}).then(name =&amp;gt; {
    console.log("It's a pleasure, Mr. " + name);
    throw new Error("Something wrong happened!")
    console.log("This will not be displayed");
}).catch(error =&amp;gt; {
    console.error("What happened? " + error.message)
}).finally(() =&amp;gt; {
    console.log("There is no coffee to drink");
})

// It's a pleasure, Mr. Freddy
// What happened? Something wrong happened!
// There is no coffee to drink

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Definitely, Promises give our code more readability, consistency and come to resolve some problems suffered from the callback functions such as the famous "Callback hell". Thus, if you are not using promises in your normal day, this is the moment.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>Asynchronous code: callback functions</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Tue, 01 Dec 2020 02:42:07 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/asynchronous-code-callback-functions-1jk8</link>
      <guid>https://forem.com/barriosdfreddy/asynchronous-code-callback-functions-1jk8</guid>
      <description>&lt;p&gt;Functions in JavaScript are not as in other languages are. In JavaScript, functions are first-class citizens. Therefore, a function can be passed as an argument, can be used as a variable value, and so on.&lt;/p&gt;

&lt;p&gt;A callback function is a subroutine, It's a piece of code which can be executed immediately or later in run-time. As asynchronous callbacks, these functions are used to notify or alert when an event happens. Normally, these functions are specified as arguments to another one that start executing some code in the background, when the background code finishes running, it calls the callback function to let know the work is done or to tell you that something has happened.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function callback() {
    console.log("Process finished!");   
}

function run(callback) {
    for (let index = 0; index &amp;lt; 10000; index++) {
        console.log("Processing...");
    }
    callback();
}


run(callback);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In simple words, an asynchronous callback is a function that can be invoked when you need to notify something happened or the work is done. Even the callbacks are old-fashioned, it's very important to know about them since a high number of APIs still use them.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>functions</category>
    </item>
    <item>
      <title>Three ways to create an object in JavaScript</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Wed, 11 Nov 2020 01:38:06 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/three-ways-to-create-an-object-in-javascript-2811</link>
      <guid>https://forem.com/barriosdfreddy/three-ways-to-create-an-object-in-javascript-2811</guid>
      <description>&lt;p&gt;In JavaScript, there are three ways to create your own objects. Taking into account that almost everything in JS It’s an object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object literals
&lt;/h2&gt;

&lt;p&gt;The simplest way to create an object in JS it’s through curly brackets &lt;code&gt;{ }&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Define and create a single object in one statement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
    name : 'Freddy',
    sayHello() {
        return `Hi ${this.name}`
    }
};

console.log(person.sayHello()) // Hi Freddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  New operator
&lt;/h2&gt;

&lt;p&gt;Using the new operator is the same thing as creating objects literally. It’s recommended to use object literals, instead of this, for simplicity and execution speed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = new Object()
person.name = 'Freddy'
person.sayHello = ()  =&amp;gt; {
   return `Hi ${this.name}`
}

console.log(person.sayHello()) // Hi Freddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, you can create an object through a constructor function. In this case, the new operator returns an instance of the function, if the function has not an explicit return statement it’ll “this “&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person (name) {
    this.name = name
    this.sayHello = function() {
        return `Hi ${this.name}`
    }
}
const person = new Person('Freddy')
console.log(person.sayHello()) // Hi Freddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Object.create() method
&lt;/h2&gt;

&lt;p&gt;In order to define and create a new object through the create &lt;br&gt;
method, we have to use the prototype from another one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = Object.create(Object.prototype)
person.name = 'Freddy'
person.sayHello = function sayHello() {
     return `Hi ${this.name}`
}


console.log(person.sayHello()) // Hi Freddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>fundamentals</category>
      <category>object</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Object destructuring assignment</title>
      <dc:creator>Barrios Freddy</dc:creator>
      <pubDate>Thu, 22 Oct 2020 16:34:43 +0000</pubDate>
      <link>https://forem.com/barriosdfreddy/object-destructuring-assignment-595n</link>
      <guid>https://forem.com/barriosdfreddy/object-destructuring-assignment-595n</guid>
      <description>&lt;p&gt;The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects. In this case, we are going to see how destructuring works for objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = {
  userName: 'fbarrios',
  firstName: 'Freddy',
  lastName: 'Barrios',
  address: {
    street: '52',
    zipcode: '110007',
}

const { firstName } = user
console.log(firstName); // Freddy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the same as…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const firstName = user.firstName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is possible to set default values, in case the property doesn’t exist or is undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { phone = '0000000' } = user
console.log(phone) // 0000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A property can be renamed when it’s destructured.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { username: displayName } = user
console.log(displayName); // Freddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can rename and set a default value for a property&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { phone: phoneNumber = '0000000' } = user
console.log(phoneNumber) // 0000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the object has another one nested it’s destructured in the following way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let { address: { street } } = user
console.log(street) // 52
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Destructuring can be used with the arguments of a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fullname({firstName, lastName}) {
  return `${firstName} ${lastName}`;
}

console.log(fullName(user)); // Freddy Barrios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Computed property names can be used with the destructuring assignment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const key = 'username'
const { [key] } = user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rest operator in object destructuring
&lt;/h3&gt;

&lt;p&gt;This operator (...) collects all properties that weren’t picked off during object destructuring&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { address, …restProperties } = user
console.log(address);
/*
{
  street: '52',
  zipcode: '110007',
}
*/ 
console.log(restProperties); /
{
  userName: 'fbarrios',
  firstName: 'Freddy',
  lastName: 'Barrios',
}
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, object destructuring is very useful and gives us another way to write better code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>destructuring</category>
      <category>fundamentals</category>
    </item>
  </channel>
</rss>
