<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Coccagerman on Medium]]></title>
        <description><![CDATA[Stories by Coccagerman on Medium]]></description>
        <link>https://medium.com/@coccagerman?source=rss-e0841585a214------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_p_iKm2c_EihWYSW_sv4Cg.jpeg</url>
            <title>Stories by Coccagerman on Medium</title>
            <link>https://medium.com/@coccagerman?source=rss-e0841585a214------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 17:40:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@coccagerman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data structures — Doubly linked lists]]></title>
            <link>https://coccagerman.medium.com/data-structures-doubly-linked-lists-15245c78d263?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/15245c78d263</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[linked-lists]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Tue, 31 Aug 2021 23:47:44 GMT</pubDate>
            <atom:updated>2021-08-31T23:47:44.407Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Doubly linked lists</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M_6VDTRNhgxNBbsH" /></figure><p>Doubly linked lists are very similar to singly linked lists, a type of data structure that store values in the form of a list. Within the list each value is considered a node, and each node is connected with the following value in the list (or null in case the element is the last in the list) through a pointer. The first element of the list is considered the head, and the last element is considered the tail. Like with arrays, the length property is defined as the number of elements the list contains.</p><p>The difference between doubly and singly linked lists, is that doubly linked lists have its nodes connected through pointers with both the previous and the next value. While singly linked lists only connect its nodes with the next value.</p><p>These double pointers approach allows doubly linked lists to perform better with certain methods compared to singly linked lists, but at a cost of consuming more memory (with doubly linked lists we need to store two pointers instead of one).</p><p>The big O of doubly linked lists methods is the following:</p><ul><li>Insertion — O(1)</li><li>Removal — O(1)</li><li>Search — O(n)</li><li>Access — O(n)</li></ul><p>Like any data structure, different methods are implemented with it in order to operate over the data. The most common ones include: push, pop, unshift, shift, get, set, insert, remove and reverse.</p><p>A full implementation of a doubly linked list might look a bit like this:</p><pre>class Node{<br>    constructor(val){<br>        this.val = val;<br>        this.next = null;<br>        this.prev = null;<br>    }<br>}<br></pre><pre>class DoublyLinkedList {<br>    constructor(){<br>        this.head = null;<br>        this.tail = null;<br>        this.length = 0;<br>    }<br>    push(val){<br>        var newNode = new Node(val);<br>        if(this.length === 0){<br>            this.head = newNode;<br>            this.tail = newNode;<br>        } else {<br>            this.tail.next = newNode;<br>            newNode.prev = this.tail;<br>            this.tail = newNode;<br>        }<br>        this.length++;<br>        return this;<br>    } <br>    pop(){<br>        if(!this.head) return undefined;<br>        var poppedNode = this.tail;<br>        if(this.length === 1){<br>            this.head = null;<br>            this.tail = null;<br>        } else {<br>            this.tail = poppedNode.prev;<br>            this.tail.next = null;<br>            poppedNode.prev = null;<br>        }<br>        this.length--;<br>        return poppedNode;<br>    }<br>    shift(){<br>        if(this.length === 0) return undefined;<br>        var oldHead = this.head;<br>        if(this.length === 1){<br>            this.head = null;<br>            this.tail = null;<br>        }else{<br>            this.head = oldHead.next;<br>            this.head.prev = null;<br>            oldHead.next = null;<br>        }<br>        this.length--;<br>        return oldHead;<br>    }<br>    unshift(val){<br>        var newNode = new Node(val);<br>        if(this.length === 0) {<br>            this.head = newNode;<br>            this.tail = newNode;<br>        } else {<br>            this.head.prev = newNode;<br>            newNode.next = this.head;<br>            this.head = newNode;<br>        }<br>        this.length++;<br>        return this;<br>    }<br>    get(index){<br>        if(index &lt; 0 || index &gt;= this.length) return null;<br>        var count, current;<br>        if(index &lt;= this.length/2){<br>            count = 0;<br>            current = this.head;<br>            while(count !== index){<br>                current = current.next;<br>                count++;<br>            }<br>        } else {<br>            count = this.length - 1;<br>            current = this.tail;<br>            while(count !== index){<br>                current = current.prev;<br>                count--;<br>            }<br>        }<br>        return current;<br>    }<br>    set(index, val){<br>        var foundNode = this.get(index);<br>        if(foundNode != null){<br>            foundNode.val = val;<br>            return true;<br>        }<br>        return false;<br>    }<br>    insert(index, val){<br>        if(index &lt; 0 || index &gt; this.length) return false;<br>        if(index === 0) return !!this.unshift(val);<br>        if(index === this.length) return !!this.push(val);</pre><pre>        var newNode = new Node(val);<br>        var beforeNode = this.get(index-1);<br>        var afterNode = beforeNode.next;</pre><pre>        beforeNode.next = newNode, newNode.prev = beforeNode;<br>        newNode.next = afterNode, afterNode.prev = newNode;<br>        this.length++;<br>        return true;<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15245c78d263" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures — Trees]]></title>
            <link>https://coccagerman.medium.com/data-structures-trees-8949ade9e9be?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/8949ade9e9be</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[trees]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Fri, 27 Aug 2021 14:30:33 GMT</pubDate>
            <atom:updated>2021-08-31T23:48:38.103Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Trees</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CT9BjVQ_YXbTPGBT" /></figure><p>Trees are a data structures that link nodes in a parent/child relationship, in the sense that there’re nodes that depend or come off other nodes.</p><p>Trees are formed by a root node (the first node on the tree), and all the nodes that come off that root are called children. The nodes at the bottom of the tree, which have no “descendants” are called leaf nodes. And the height of the tree is determined by the amount of parent/child connection it has.</p><ul><li>Root: Top node in a tree.</li><li>Parent: A node that has descendants nodes.</li><li>Child: A node that descends from another node.</li><li>Siblings: nodes that descend from the same parent.</li><li>Leafs: Nodes with no children.</li><li>Edge: The connection of one node with another.</li></ul><p>Unlike linked lists or arrays, trees are non linear, in the sense that the program flow can follow different directions within the data structure and hence arrive at different values. While on linked lists or arrays, the program can only iterate the data structure from one extreme of it to the other, always following the same path.</p><p>An important requirement for tree formation is that the only valid connection between nodes is from parent to child . Connection between siblings or from child to parent are not allowed in trees (these type of connections form graphs, a different type of data structure). Another important requirement is that trees must have a one and only root.</p><p>Some examples of tree usage in programming are:</p><ul><li>The DOM model.</li><li>Situation analysis in artificial intelligence.</li><li>File folders in operating systems.</li></ul><p>There’re many different types of trees. In each type of tree, values may be organized following different patterns that make this data structure more suitable to use when facing different kinds of problems.</p><p>The most common tree types are:</p><h3>Binary trees</h3><p>In binary trees, each node has a maximum of two children. Nodes can’t have more than two children.</p><h3>Binary search trees (BST)</h3><p>Binary search trees are just like binary trees but information within them is ordered in away that makes them a suitable data structure for searching. In BST, values are ordered in a way that each node that descends to the left side of its parent must have a value less than its parent, and each node that descends to the right side of its parent must have a value bigger than its parent.</p><p>This order in its values make this data structure great for searching, since on every level of the tree we can identify if the value being looked for is greater or less than the parent node, and from that comparison progressively discard roughly half of the data until we reach our value.</p><p>When inserting or deleting values, the algorithm will follow the following steps:</p><ul><li>Check if there’s a root node.</li><li>If there is, check if the value to add/delete is greater or less than the node.</li><li>If it is less, check if there is a node to the left and repeat the previous operation. If there’s not, add/remove the node in that position.</li><li>If it is greater, check if there is a node to the right and repeat the previous operation. If there’s not, add/remove the node in that position.</li></ul><p>Searching in BST is very similar, only that instead of adding/deleting values we check the nodes for equality with the value we’re looking for.</p><p>The big O complexity of these operations is logarithmic (log(n)), though it’s important to recognize that for this complexity to be achieved, the tree must have a balanced structure so that in each search step approximately half of the data can be “discarded”. If more values are stored to one side or another of three, the efficiency of the data structure is affected.</p><p>An implementation of a BST might look like this:</p><pre>class Node {<br>    constructor(value){<br>        this.value = value;<br>        this.left = null;<br>        this.right = null;<br>    }<br>}</pre><pre>class BinarySearchTree {<br>    constructor(){<br>        this.root = null;<br>    }<br>    insert(value){<br>        var newNode = new Node(value);<br>        if(this.root === null){<br>            this.root = newNode;<br>            return this;<br>        }<br>        var current = this.root;<br>        while(true){<br>            if(value === current.value) return undefined;<br>            if(value &lt; current.value){<br>                if(current.left === null){<br>                    current.left = newNode;<br>                    return this;<br>                }<br>                current = current.left;<br>            } else {<br>                if(current.right === null){<br>                    current.right = newNode;<br>                    return this;<br>                } <br>                current = current.right;<br>            }<br>        }<br>    }<br>    find(value){<br>        if(this.root === null) return false;<br>        var current = this.root,<br>            found = false;<br>        while(current &amp;&amp; !found){<br>            if(value &lt; current.value){<br>                current = current.left;<br>            } else if(value &gt; current.value){<br>                current = current.right;<br>            } else {<br>                found = true;<br>            }<br>        }<br>        if(!found) return undefined;<br>        return current;<br>    }<br>    contains(value){<br>        if(this.root === null) return false;<br>        var current = this.root,<br>            found = false;<br>        while(current &amp;&amp; !found){<br>            if(value &lt; current.value){<br>                current = current.left;<br>            } else if(value &gt; current.value){<br>                current = current.right;<br>            } else {<br>                return true;<br>            }<br>        }<br>        return false;<br>    }<br>}</pre><h3>Tree traversal</h3><p>We saw that BST are great for searching values. But how do we search for values in regular trees, where values aren’t in order? In this cases we just need to traverse the tree (check every node within it), and turns out there’re many ways in which we can approach this task.</p><h3>Breadth first search (BFS)</h3><p>In breadth first search, each “level” of the tree is progressively checked for the value we’re looking for. The algorithm starts at the root, the goes on to the root’s children, then to the root’s children’s children, and so on until it gets to the last level or “generation” of the tree, scanning the tree in a horizontal way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UHWxTT2ONXaWxUly" /></figure><p>The algorithm should follow the following steps:</p><ul><li>Create a queue and a value to store the values of the visited nodes. (Remember that in queue elements are added and removed following a FIFO pattern — First in, first out.)</li><li>Place the root node in the queue</li><li>Loop as long as there’s anything in the queue and: 1) Dequeue a node from the queue and push the node’s value into the variable that stores the visited nodes. 2) If there’s a left property on the dequeued node, add it to the queue. 3) If there’s a right property on the dequeued node, add it to the queue</li><li>Return the variable that stores the values.</li></ul><p>A possible implementation could be as following:</p><pre>BFS(){<br>        var node = this.root,<br>            data = [],<br>            queue = [];<br>        queue.push(node);</pre><pre>        while(queue.length){<br>           node = queue.shift();<br>           data.push(node.value);<br>           if(node.left) queue.push(node.left);<br>           if(node.right) queue.push(node.right);<br>        }<br>        return data;<br>    }</pre><h3>Depth first search (DFS)</h3><p>In depth first search each “branch” of the tree is vertically checked for the value we’re looking for. The algorithm starts at the root, then to one of its children, then to one of its children’s children, and so on until it reaches the leaf level of the branch. Once there, the algorithm backtracks to the first parent node who has an unvisited children and scans through that branch, and repeats the process until the whole tree has been visited.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/0*OWjwQ0Z2vgGLmm-x" /></figure><p>DFS can be executed in different orders, which will affect the way the tree is traversed.</p><p>Example image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/0*kPtsNtIZ0Uk1yf-1" /></figure><h4>Preorder</h4><p>In Preorder DFS, the tree wil be traversed following a Root, Left, Right order. Using the tree in the example image, the output would be “1 2 4 5 3”.</p><p>An implementation could look as following:</p><pre>DFSPreOrder(){<br>        var data = [];<br>        function traverse(node){<br>            data.push(node.value);<br>            if(node.left) traverse(node.left);<br>            if(node.right) traverse(node.right);<br>        }<br>        traverse(this.root);<br>        return data;<br>}</pre><h4>Post order</h4><p>In Postorder DFS, the tree wil be traversed following a Left, Right, Root order. Using the tree in the example image, the output would be “4 5 2 3 1”.</p><p>An implementation could look as following:</p><pre>DFSPostOrder(){<br>        var data = [];<br>        function traverse(node){<br>            if(node.left) traverse(node.left);<br>            if(node.right) traverse(node.right);<br>            data.push(node.value);<br>        }<br>        traverse(this.root);<br>        return data;<br>}</pre><h4>In order</h4><p>In In order DFS, the tree wil be traversed following a Left, Root, Right order. Using the tree in the example image, the output would be “4 2 5 1 3 “.</p><p>An implementation could look as following:</p><pre>DFSInOrder(){<br>        var data = [];<br>        function traverse(node){<br>            if(node.left) traverse(node.left);<br>            data.push(node.value);<br>            if(node.right) traverse(node.right);<br>        }<br>        traverse(this.root);<br>        return data;<br>}</pre><h3>Depth first search (DFS) vs Breadth first search (BFS)</h3><p>How do we choose between DFS and BFS? The difference between them relies only in the use of memory the algorithms will have (space complexity), since the time complexity of both is the same.</p><p>Generally speaking, when we have very “wide” trees (trees that have many branches), we will go with DFS; and when we have very “long” trees (trees that have many father-child relationships within each branch) we will go with BFS.</p><p>Since BFS traverses trees in a horizontal way, in very wide trees the algorithm will have to use a lot of memories to store all the values of each tree “level”, and the same goes with very long trees and DFS.</p><h3>Heaps</h3><p>Heaps are another type of trees that have some particular rules. There are two main types of heaps MaxHeaps and MinHeaps. In MaxHeaps, parent nodes are always greater than its children, and in MinHeaps, parent nodes are always smaller than its children.</p><p>In this data structure there’re no guarantees between siblings, meaning that nodes at the same “level” don’t follow any rule besides being higher/lower than its parent. Also, heaps are as compact as possible, meaning each level contains all the nodes it can contain with no empty spaces, and new children are put into the left spaces of the tree first.</p><p>Heaps, and in particular binary heaps are frequently used to implement priority queues, which at the same time are frequently used in well known algorithm such as dijkstra’s pathfinding algorithm. Priority queues are a type of data structure in which each element has an associated priority and elements with a higher priority are presented first.</p><p>Heaps have a log (n) complexity for insertion and removal operations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8949ade9e9be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures — Queues]]></title>
            <link>https://coccagerman.medium.com/data-structures-queues-497833468a2d?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/497833468a2d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[codin]]></category>
            <category><![CDATA[queue]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Sun, 22 Aug 2021 17:12:19 GMT</pubDate>
            <atom:updated>2021-08-22T17:12:19.555Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Queues</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UUGbSFSddSQMTUbQ" /></figure><p>Queues are a data structure that store information in the form of a list, allowing only adding and removing elements under a FIFO pattern (first in, first out). In queues, elements can’t be added or removed out of order, they always have to follow the FIFO pattern.</p><p>Some examples of queues usage are:</p><ul><li>Background tasks.</li><li>Printing/task processing.</li></ul><p>There’s more than one way to implement a stack, but probably the simplest is using an array with its push and shift methods. If we only use push and shift for adding and deleting elements, we’ll always follow the FIFO pattern and so operating over it like a queue.</p><p>Another way is to implement it like a linked list, which may look like this:</p><pre>class Node {<br>    constructor(value){<br>        this.value = value;<br>        this.next = null;<br>    }<br>}</pre><pre>class Queue {<br>    constructor(){<br>        this.first = null;<br>        this.last = null;<br>        this.size = 0;<br>    }<br>    enqueue(val){<br>        var newNode = new Node(val);<br>        if(!this.first){<br>            this.first = newNode;<br>            this.last = newNode;<br>        } else {<br>            this.last.next = newNode;<br>            this.last = newNode;<br>        }<br>        return ++this.size;<br>    }</pre><pre>    dequeue(){<br>        if(!this.first) return null;</pre><pre>        var temp = this.first;<br>        if(this.first === this.last) {<br>            this.last = null;<br>        }<br>        this.first = this.first.next;<br>        this.size--;<br>        return temp.value;<br>    }<br>}</pre><p>The big O of queue methods is the following:</p><ul><li>Insertion — O(1)</li><li>Removal — O(1)</li><li>Searching — O(n)</li><li>Access — O(n)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=497833468a2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures — Stacks]]></title>
            <link>https://coccagerman.medium.com/data-structures-stacks-7ec3c3247af1?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ec3c3247af1</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[stack]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Sun, 22 Aug 2021 17:02:26 GMT</pubDate>
            <atom:updated>2021-08-22T17:02:26.387Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Stacks</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GuZtwdJgeZBzog12" /></figure><p>Stacks are a data structure that store information in the form of a list, allowing only adding and removing elements under a LIFO pattern (last in, first out). In stacks, elements can’t be added or removed out of order, they always have to follow the LIFO pattern.</p><p>Some examples of stack usage are:</p><ul><li>JavaScript&#39;s call stack.</li><li>Managing function invocations in various programming languages.</li><li>The undo/redo functionality many applications offer.</li></ul><p>There’s more than one way to implement a stack, but probably the simplest is using an array with its push and pop methods. If we only use pop and push for adding and deleting elements, we’ll always follow the LIFO pattern and so operating over it like a stack.</p><p>Another way is to implement it like a linked list, which may look like this:</p><pre>class Node {<br>    constructor(value){<br>        this.value = value;<br>        this.next = null;<br>    }<br>}</pre><pre>class Stack {<br>    constructor(){<br>        this.first = null;<br>        this.last = null;<br>        this.size = 0;<br>    }<br>    push(val){<br>        var newNode = new Node(val);<br>        if(!this.first){<br>            this.first = newNode;<br>            this.last = newNode;<br>        } else {<br>            var temp = this.first;<br>            this.first = newNode;<br>            this.first.next = temp;<br>        }<br>        return ++this.size;<br>    }<br>    pop(){<br>        if(!this.first) return null;<br>        var temp = this.first;<br>        if(this.first === this.last){<br>            this.last = null;<br>        }<br>        this.first = this.first.next;<br>        this.size--;<br>        return temp.value;<br>    }<br>}</pre><p>The big O of stack methods is the following:</p><ul><li>Insertion — O(1)</li><li>Removal — O(1)</li><li>Searching — O(n)</li><li>Access — O(n)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ec3c3247af1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures — Singly linked lists]]></title>
            <link>https://coccagerman.medium.com/data-structures-singly-linked-lists-37b367ceb4a8?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/37b367ceb4a8</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[javasctipt]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Sun, 22 Aug 2021 15:21:16 GMT</pubDate>
            <atom:updated>2021-08-22T15:21:16.789Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Singly linked lists</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xXRhCAfmtEN4HeY3" /></figure><p>Singly linked lists are a type of data structure that store values in the form of a list. Within the list each value is considered a node, and each node is connected with the following value in the list (or null in case the element is the last in the list) through a pointer.</p><p>The first element of the list is considered the head, and the last element is considered the tail. Like with arrays, the length property is defined as the number of elements the list contains.</p><p>The main differences compared with arrays are the following:</p><ul><li>Lists don’t have indexes. Each value only “knows” the values to which its connected though pointers.</li><li>Since lists don’t have indexes, we can’t access values randomly. When we want to access a value, we always have to look for it iterating through the list starting from its head or tail.</li><li>The good thing of not having indexes, is that insertion/deletion in any part of the list is more efficient than with arrays. We just have to redirect the pointers of the “neighbor” values, while in arrays or later values need to be reindexed.</li></ul><p>Like any data structure, different methods are implemented with it in order to operate over the data. The most common ones include: push, pop, unshift, shift, get, set, insert, remove and reverse.</p><h3>Push()</h3><ul><li>A function that takes a value as input and creates a new node with it.</li><li>If there’s no head (empty list), it will assign the new node as head and tail.</li><li>If the list isn’t empty, it assigns the new node as tail and connects the previous head with the new node with a “next” pointer.</li><li>It increments the length property by one and returns the list with the added element.</li></ul><p>A possible implementation could be the following:</p><pre>push(val){<br>        var newNode = new Node(val);<br>        if(!this.head){<br>            this.head = newNode;<br>            this.tail = this.head;<br>        } else {<br>            this.tail.next = newNode;<br>            this.tail = newNode;<br>        }<br>        this.length++;<br>        return this;<br>    }</pre><h3>Pop()</h3><ul><li>A function that erases the last value (tail) of the list.</li><li>If the list is empty it returns undefined.</li><li>It loops through the list until it reaches the tail and erases it.</li><li>It modifies the pointer of the previous value so it points to null, and assigns the previous value as tail.</li><li>It decrements the length property by one and returns the erased element.</li></ul><p>A possible implementation could be the following:</p><pre>pop(){<br>        if(!this.head) return undefined;<br>        var current = this.head;<br>        var newTail = current;<br>        while(current.next){<br>            newTail = current;<br>            current = current.next;<br>        }<br>        this.tail = newTail;<br>        this.tail.next = null;<br>        this.length--;<br>        if(this.length === 0){<br>            this.head = null;<br>            this.tail = null;<br>        }<br>        return current;</pre><pre>    }</pre><h3>unshift()</h3><ul><li>A function that takes a value and adds it to the beginning of the list.</li><li>If there’s no head (empty list), it will assign the new node as head and tail.</li><li>If the list isn’t empty, it assigns the new node as the head.</li></ul><p>A possible implementation could be the following:</p><pre>unshift(val){<br>        var newNode = new Node(val);<br>        if(!this.head) {<br>            this.head = newNode;<br>            this.tail = this.head;<br>        } else {<br>            newNode.next = this.head;<br>            this.head = newNode;<br>        }<br>        this.length++;<br>        return this;<br>    }</pre><h3>Shift()</h3><ul><li>A function that erases the first value (head) of the list.</li><li>If the list is empty it returns undefined.</li><li>It assigns the head property to the next value, decreases the length property by one and returns the erased element value.</li></ul><p>A possible implementation could be the following:</p><pre>shift(){<br>        if(!this.head) return undefined;<br>        var currentHead = this.head;<br>        this.head = currentHead.next;<br>        this.length--;<br>        if(this.length === 0){<br>            this.tail = null;<br>        }<br>        return currentHead;<br>    }</pre><h3>get()</h3><ul><li>A function that takes a number (index or position) and retrieves the node with the given position in the list.</li><li>If the index is less than zero or greater than or equal to the length of the list, it returns null.</li><li>Else it loops through the list until the given index is reached and returns that value.</li></ul><p>A possible implementation could be the following:</p><pre>get(index){<br>        if(index &lt; 0 || index &gt;= this.length) return null;<br>        var counter = 0;<br>        var current = this.head;<br>        while(counter !== index){<br>            current = current.next;<br>            counter++;<br>        }<br>        return current;<br>    }</pre><h3>set()</h3><ul><li>A function that takes a number (index or position) and a value and modifies the value of the node with the given position in the list.</li><li>If the value isn’t found it returns false. Else, if the operation is completed, it returns true.</li></ul><p>A possible implementation could be the following:</p><pre>set(index, val){<br>        var foundNode = this.get(index);<br>        if(foundNode){<br>            foundNode.val = val;<br>            return true;<br>        }<br>        return false;<br>    }</pre><h3>insert()</h3><ul><li>A function that takes a number (index or position) and a value and inserts the value as a new node with the given position in the list.</li><li>If the value is less than zero or greater than the list’s length, return undefined.</li><li>If the index is the same as the length, push a new node to the list. If the index is zero, unshift a new node.</li><li>Otherwise, using the get method we access the node previous to the position we want to enter the new node, and redirect its pointer to the new node. We direct the pointer of the new node to the node that follows in order.</li><li>Increment the list length and return true.</li></ul><p>A possible implementation could be the following:</p><pre>if(index &lt; 0 || index &gt; this.length) return false;<br>        if(index === this.length) return !!this.push(val);<br>        if(index === 0) return !!this.unshift(val);</pre><pre>        var newNode = new Node(val);<br>        var prev = this.get(index - 1);<br>        var temp = prev.next;<br>        prev.next = newNode;<br>        newNode.next = temp;<br>        this.length++;<br>        return true;</pre><h3>remove()</h3><ul><li>A function that takes a number (index or position) removes the node the node with the given position in the list.</li><li>If the value is less than zero or greater than the list’s length, return undefined.</li><li>If the index is the same as the length, pop the last element. If the index is zero, shift the first element.</li><li>Otherwise, using the get method we access the node previous to the position we want to remove, and redirect its pointer to the node next to the one we want to remove.</li><li>Decrement the list length and return the value of the erased node.</li></ul><p>A possible implementation could be the following:</p><pre>if(index &lt; 0 || index &gt;= this.length) return undefined;<br>        if(index === 0) return this.shift();<br>        if(index === this.length - 1) return this.pop();<br>        var previousNode = this.get(index - 1);<br>        var removed = previousNode.next;<br>        previousNode.next = removed.next;<br>        this.length--;<br>        return removed;</pre><h3>reverse()</h3><ul><li>A function that reverses the order of the list in place.</li><li>Swaps head and tail.</li><li>Creates variables called next and prev.</li><li>Creates a variable called node and initializes it to the head property.</li><li>Then loop through the list and: 1) Set next variable to be the next property on whatever node the loop is. 2) Set the next property on the node to be whatever prev is. 3) Set prev to be the value of the node variable. 4) Set the node variable to be the value of the next variable.</li><li>Once the loop is finished, return the list.</li></ul><p>A possible implementation could be the following:</p><pre>var node = this.head;<br>      this.head = this.tail;<br>      this.tail = node;<br>      var next;<br>      var prev = null;<br>      for(var i = 0; i &lt; this.length; i++){<br>        next = node.next;<br>        node.next = prev;<br>        prev = node;<br>        node = next;<br>      }<br>      return this;</pre><p>A full implementation of a singly linked list could be the following:</p><pre>class Node{<br>    constructor(val){<br>        this.val = val;<br>        this.next = null;<br>    }<br>}</pre><pre>class SinglyLinkedList{<br>    constructor(){<br>        this.head = null;<br>        this.tail = null;<br>        this.length = 0;<br>    }<br>    push(val){<br>        var newNode = new Node(val);<br>        if(!this.head){<br>            this.head = newNode;<br>            this.tail = this.head;<br>        } else {<br>            this.tail.next = newNode;<br>            this.tail = newNode;<br>        }<br>        this.length++;<br>        return this;<br>    }<br>    pop(){<br>        if(!this.head) return undefined;<br>        var current = this.head;<br>        var newTail = current;<br>        while(current.next){<br>            newTail = current;<br>            current = current.next;<br>        }<br>        this.tail = newTail;<br>        this.tail.next = null;<br>        this.length--;<br>        if(this.length === 0){<br>            this.head = null;<br>            this.tail = null;<br>        }<br>        return current;<br>    }<br>    shift(){<br>        if(!this.head) return undefined;<br>        var currentHead = this.head;<br>        this.head = currentHead.next;<br>        this.length--;<br>        if(this.length === 0){<br>            this.tail = null;<br>        }<br>        return currentHead;<br>    }<br>    unshift(val){<br>        var newNode = new Node(val);<br>        if(!this.head) {<br>            this.head = newNode;<br>            this.tail = this.head;<br>        }<br>        newNode.next = this.head;<br>        this.head = newNode;<br>        this.length++;<br>        return this;<br>    }<br>    get(index){<br>        if(index &lt; 0 || index &gt;= this.length) return null;<br>        var counter = 0;<br>        var current = this.head;<br>        while(counter !== index){<br>            current = current.next;<br>            counter++;<br>        }<br>        return current;<br>    }<br>    set(index, val){<br>        var foundNode = this.get(index);<br>        if(foundNode){<br>            foundNode.val = val;<br>            return true;<br>        }<br>        return false;<br>    }<br>    insert(index, val){<br>        if(index &lt; 0 || index &gt; this.length) return false;<br>        if(index === this.length) return !!this.push(val);<br>        if(index === 0) return !!this.unshift(val);</pre><pre>        var newNode = new Node(val);<br>        var prev = this.get(index - 1);<br>        var temp = prev.next;<br>        prev.next = newNode;<br>        newNode.next = temp;<br>        this.length++;<br>        return true;<br>    }<br>    remove(index){<br>        if(index &lt; 0 || index &gt;= this.length) return undefined;<br>        if(index === 0) return this.shift();<br>        if(index === this.length - 1) return this.pop();<br>        var previousNode = this.get(index - 1);<br>        var removed = previousNode.next;<br>        previousNode.next = removed.next;<br>        this.length--;<br>        return removed;<br>    }<br>    reverse(){<br>      var node = this.head;<br>      this.head = this.tail;<br>      this.tail = node;<br>      var next;<br>      var prev = null;<br>      for(var i = 0; i &lt; this.length; i++){<br>        next = node.next;<br>        node.next = prev;<br>        prev = node;<br>        node = next;<br>      }<br>      return this;<br>    }<br>    print(){<br>        var arr = [];<br>        var current = this.head<br>        while(current){<br>            arr.push(current.val)<br>            current = current.next<br>        }<br>        console.log(arr);<br>    }<br>}</pre><h3>Complexity</h3><p>Singly linked lists functions have the following complexities:</p><ul><li>Insertion — O(1)</li><li>Removal — O(n)</li><li>Search — O(n)</li><li>Access — O(n)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37b367ceb4a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures — Objects]]></title>
            <link>https://coccagerman.medium.com/data-structures-objects-b8e927038df2?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8e927038df2</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[objects]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 17:38:49 GMT</pubDate>
            <atom:updated>2021-08-20T17:38:49.340Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Objects</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zukCocKKSyQEo0ur" /></figure><p>In JavaScript, an object is a collection of key-value pairs, similar to a map, dictionary, or hash-table in other programming languages.</p><ul><li>An Object is a collection of properties and methods.</li><li>A Property is a key-value pair that contains a name and a value.</li><li>A Property Name is a unique value that can be coerced to a string that points to a value.</li><li>A Property Value can be any value, including other objects.</li><li>Methods are functions that are declared within the object and later on can only be called through this object.</li></ul><p>In javascript objects come with many built-in methods that allow us to perform different operations and get information from a given object.</p><p>Objects are a good way to group together data that have something in common or are somehow related. Also, thanks to the fact that property names are unique, objects come in handy when we have to separate data based on an unique condition.</p><p>In javascript objects are declared within braces and each key-value pair is separated by a comma. Each key-value pair is built with a property name, a colon and the property value or method.</p><pre>let obj = {<br>    name: &#39;german&#39;,<br>    ladrar: function() {console.log(&#39;guau!&#39;)}<br>}</pre><p>For accessing values we just call the object name followed by a point and the property name.</p><pre>obj.name</pre><p>If we want to execute a method, we do the same but followed by parenthesis.</p><pre>obj.ladrar()</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8e927038df2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures — Arrays]]></title>
            <link>https://coccagerman.medium.com/data-structures-arrays-99d0309cbc29?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/99d0309cbc29</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 17:13:26 GMT</pubDate>
            <atom:updated>2021-08-20T17:13:26.288Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data structures — Arrays</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0zv8cLYzCAdaSwP_" /></figure><p>An array is a collection of items stored at contiguous memory locations.</p><p>Each item can be accessed through its index (position) number. Arrays always start at index 0, so in an array of 4 elements we could access the 3rd element using the index number 2.</p><p>The length property of an array is defined as the number of elements it contains. If the array contains 4 elements, we can say the array has a length of 4.</p><p>In some programming languages, the user can only store values of the same type in a same array and the length of the array has to be defined at the moment of its creation and can’t be modified afterwards. In javascript that’s not the case, as we can store values of any type in the same array and the length of it can be dynamic (grow or shrink as much as necessary).</p><p>Any data type can be stored in an array, and that includes arrays too. An array that has other arrays within itself is called a multidimensional array.</p><p>In javascript arrays come with many built in properties and methods we can use with different purposes such as adding or deleting items from the array, sorting it, filtering its values, know it’s length and so on.</p><p>In javascript, arrays are represented with brackets and its values are separated by comas.</p><pre>let array = [ value1, value2, value3]</pre><p>As we mentioned, in arrays each element has an index defined by its position in the array. When we add a new item at the end of the array, it just takes the index number that follows the previous item in the array. But when we add/delete a new item at the beginning or the middle of the array, the indexes of all the elements that come after the element added/deleted have to be changed. This has a computational cost, and is one of the weaknesses of this data structure.</p><p>Arrays are useful when we have to add/delete values from the end of the data structure, but when we need to add/delete from any part of it, there are other data structures that perform more efficiently.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99d0309cbc29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data structures]]></title>
            <link>https://coccagerman.medium.com/data-structures-1e7929962d87?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e7929962d87</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 15:03:48 GMT</pubDate>
            <atom:updated>2021-08-20T15:03:48.232Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0pDkBM0nZstMPCj5" /></figure><p>In <a href="https://en.wikipedia.org/wiki/Computer_science">computer science</a>, a <strong>data structure</strong> is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.</p><p>Different types of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, relational databases commonly use <a href="https://en.wikipedia.org/wiki/B-tree">B-tree</a> indexes for data retrieval, while <a href="https://en.wikipedia.org/wiki/Compiler">compiler</a> implementations usually use <a href="https://en.wikipedia.org/wiki/Hash_table">hash tables</a> to look up identifiers. Usually, efficient data structures are key to designing efficient <a href="https://en.wikipedia.org/wiki/Algorithm">algorithms</a>.</p><p>There are numerous types of data structures, generally built upon simpler <a href="https://en.wikipedia.org/wiki/Primitive_data_type">primitive data types</a>:</p><ul><li>An <a href="https://en.wikipedia.org/wiki/Array_data_structure"><em>array</em></a> is a number of elements in a specific order, typically all of the same type (depending on the language, individual elements may either all be forced to be the same type, or may be of almost any type). Elements are accessed using an integer index to specify which element is required. Typical implementations allocate contiguous memory words for the elements of arrays (but this is not always a necessity). Arrays may be fixed-length or resizable.</li><li>A <a href="https://en.wikipedia.org/wiki/Linked_list"><em>linked list</em></a> (also just called <em>list</em>) is a linear collection of data elements of any type, called nodes, where each node has itself a value, and points to the next node in the linked list. The principal advantage of a linked list over an array is that values can always be efficiently inserted and removed without relocating the rest of the list. Certain other operations, such as <a href="https://en.wikipedia.org/wiki/Random_access">random access</a> to a certain element, are however slower on lists than on arrays.</li><li>A <a href="https://en.wikipedia.org/wiki/Record_(computer_science)"><em>record</em></a> (also called <em>tuple</em> or <em>struct</em>) is an <a href="https://en.wikipedia.org/wiki/Aggregate_data">aggregate data</a> structure. A record is a value that contains other values, typically in fixed number and sequence and typically indexed by names. The elements of records are usually called <em>fields</em> or <em>members</em>.</li><li>A <a href="https://en.wikipedia.org/wiki/Union_(computer_science)"><em>union</em></a> is a data structure that specifies which of a number of permitted primitive types may be stored in its instances, e.g. <em>float</em> or <em>long integer</em>. Contrast with a <a href="https://en.wikipedia.org/wiki/Record_(computer_science)">record</a>, which could be defined to contain a float <em>and</em> an integer; whereas in a union, there is only one value at a time. Enough space is allocated to contain the widest member datatype.</li><li>A <a href="https://en.wikipedia.org/wiki/Tagged_union"><em>tagged union</em></a> (also called <a href="https://en.wikipedia.org/wiki/Variant_type"><em>variant</em></a>, <em>variant record</em>, <em>discriminated union</em>, or <em>disjoint union</em>) contains an additional field indicating its current type, for enhanced type safety.</li><li>An <a href="https://en.wikipedia.org/wiki/Object_(computer_science)"><em>object</em></a> is a data structure that contains data fields, like a record does, as well as various <a href="https://en.wikipedia.org/wiki/Method_(computer_programming)">methods</a> which operate on the data contents. An object is an in-memory instance of a class from a taxonomy. In the context of <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">object-oriented programming</a>, records are known as <a href="https://en.wikipedia.org/wiki/Plain_old_data_structure">plain old data structures</a> to distinguish them from objects.</li></ul><p>In addition, <a href="https://en.wikipedia.org/wiki/Graph_(computer_science)"><em>graphs</em></a> and <a href="https://en.wikipedia.org/wiki/Binary_trees"><em>binary trees</em></a> are other commonly used data structures.</p><p>JavaScript has primitive (built in) and non-primitive (not built in) data structures. <strong>Primitive data structures</strong> and data types are native to the programming language. These include boolean, null, number, string, etc. <strong>Non-primitive data structures</strong> are not defined by the programming language but rather by the programmer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e7929962d87" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prototypes, inheritance and why people say that in javascript everything is an object.]]></title>
            <link>https://coccagerman.medium.com/prototypes-inheritance-and-why-people-say-that-in-javascript-everything-is-an-object-e5ae1464fbf8?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5ae1464fbf8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[prototype]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 01:28:36 GMT</pubDate>
            <atom:updated>2021-12-17T15:10:48.714Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/802/0*otJBTvYTzFn3wR06.jpg" /></figure><p>Did you ever wonder how strings, arrays or objects “know” the methods each of them have? How does a string know it can .toUpperCase() or an array that it can .sort()? We never defined these methods manually, right?</p><p>The answer is that these methods come <em>built-in </em>within each type of data structure thanks to something called <strong>prototype inheritance</strong>.</p><p>In JavaScript, an object can <em>inherit</em> properties of another object. The object from where the properties are inherited is named <em>prototype</em>. Objects can inherit properties from other objects — the prototypes.</p><p>You’re probably wondering: why the need for inheritance in the first place? Inheritance solves the problem of data and logic duplication. By inheriting, objects can share properties and methods.</p><h4>How to access prototype’s properties and methods</h4><p>When we try to access a property of an object, the property is not only searched in the object itself but also in the prototype of the object, in the prototype of the prototype, and so on until a property is found that matches the name or the end of the prototype chain is reached. If the property or method isn’t found nowhere in the prototype chain, only then javascript will return undefined.</p><p>Every object in JavaScript has an internal property called [[Prototype]]. The double square brackets that enclose [[Prototype]] signify that it is an internal property, and cannot be accessed directly in code. To find the [[Prototype]] of an object, we will use the Object.getPrototypeOf() method.</p><p>The output will consist of several built-in properties and methods.</p><pre>Output</pre><pre>{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, …}</pre><p>Another way to find the [[Prototype]] is through the __proto__ property. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto">__proto__</a> is a property that exposes the internal [[Prototype]] of an object.</p><p>It is important to note that .__proto__ is a legacy feature and should not be used in production code, and it is not present in every modern browser. The output will be the same as if you had used getPrototypeOf().</p><p>Keep in mind that prototypes can also be changed and modified through different methods.</p><h4>The prototype chain</h4><p>At the end of the prototype chain is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype">Object.prototype</a>. All objects inherit the properties and methods of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>. Any attempt to search beyond the end of the chain results in null.</p><p>If you look for the prototype of the prototype of an array, a function or a string, you’ll see it’s an object. And that’s because in javascript all objects are descendants or instances of <strong>Object.prototype</strong>, which is an object that sets properties and methods to all other javascript’s data types.</p><p>Each type of prototype (for example array prototype) defines it’s own methods an properties, and in some cases override the Object.prototype methods an properties (that’s why arrays have methods that objects don’t). But under the hood and going up the ladder of the prototype chain, <strong>everything in javascript is built upon the Object.prototype</strong>.</p><h4>A note on classes</h4><p>JavaScript is a <strong>prototype-based language</strong>, meaning object properties and methods can be shared through generalized objects that have the ability to be cloned and extended. This is known as prototypical inheritance and differs from class inheritance. Among popular object-oriented programming languages, JavaScript is relatively unique, as other prominent languages such as PHP, Python, and Java are class-based languages, which instead define classes as blueprints for objects.</p><p>When it comes to inheritance, JavaScript has only one structure: objects. Each object has a private property (referred to as its [[Prototype]]) that maintains a link to another object called its prototype. That prototype object has its own prototype, and so on until an object whose prototype is null is reached. By definition, null has no prototype, and acts as the final link in this chain of prototypes. Almost all objects in JavaScript are instances of Object at the top of the prototype chain.</p><p>Even though this is often seen as one of JavaScript’s main weaknesses, the prototype inheritance model is in fact more powerful than the classic class based model, because given the fact that prototypes can be manually changed it’s quite simple to build a classic model from a prototype model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5ae1464fbf8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Constructor functions in javascript]]></title>
            <link>https://coccagerman.medium.com/constructor-functions-in-javascript-1857d97ff92c?source=rss-e0841585a214------2</link>
            <guid isPermaLink="false">https://medium.com/p/1857d97ff92c</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Coccagerman]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 01:10:17 GMT</pubDate>
            <atom:updated>2021-08-20T01:10:17.655Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*hLi992xjIZ0-fLEB.png" /></figure><p><strong>Constructor functions</strong> are functions that are used to construct new objects. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new">new operator</a> is used to create new instances based off a constructor function. There are some built-in JavaScript constructors, such as new Array() and new Date(), but we can also create our own custom templates from which to build new objects.</p><p>To begin, a constructor function is just a regular function. It becomes a constructor when it is called on by an instance with the new keyword. In JavaScript, we capitalize the first letter of a constructor function by convention.</p><p>function Hero(name, level) {<br> this.name = name;<br> this.level = level;<br>}</p><p>The this keyword will refer to the new instance that is created, so setting this.name to the name parameter ensures the new object will have a name property set.</p><p>Now we can create a new instance with new.</p><pre>let hero1 = new Hero(&#39;Bjorn&#39;, 1);</pre><p>Now if we get the [[Prototype]] of hero1, we will be able to see the constructor as Hero(). (Remember, this has the same input as hero1.__proto__, but is the proper method to use.)</p><pre>Object.getPrototypeOf(hero1);</pre><p>// Output</p><pre>constructor: ƒ Hero(name, level)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1857d97ff92c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>