<?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: Huseyin Gumus</title>
    <description>The latest articles on Forem by Huseyin Gumus (@erhaneth).</description>
    <link>https://forem.com/erhaneth</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%2F991063%2Faebf469b-3e8e-4197-b84f-e69c388b2eb8.png</url>
      <title>Forem: Huseyin Gumus</title>
      <link>https://forem.com/erhaneth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/erhaneth"/>
    <language>en</language>
    <item>
      <title>The Power of Binding: A Beginner's Guide to Bind method in JavaScript</title>
      <dc:creator>Huseyin Gumus</dc:creator>
      <pubDate>Mon, 26 Dec 2022 07:12:06 +0000</pubDate>
      <link>https://forem.com/erhaneth/what-is-binding--hao</link>
      <guid>https://forem.com/erhaneth/what-is-binding--hao</guid>
      <description>&lt;p&gt;In JavaScript, "binding" refers to the process of setting up a connection between an object and a function. There are several ways to bind an object to a function in JavaScript, but the most common way using "bind" method.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might use the "bind" method to bind an object to 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;const object = {
  name: 'Raskolnikov',
  sayHello: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

const boundFunction = object.sayHello.bind(object);

boundFunction(); //Output: "Hello, my name is Raskolnikov"

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

&lt;/div&gt;



&lt;p&gt;In this example, the "sayHello" function is bound to the "object" using the "bind" method. This means that when the "boundFunction" is called, the value of "this" within the function will be set to the "object" object. As a result, calling "bound" will output "Hello, my name is Raskolnikov".&lt;/p&gt;

&lt;p&gt;There are other ways to bind an object to a function in JavaScript, such as using the "call" and "apply" methods. However, the "bind" method is the mostt common and is often the easiest to use.&lt;/p&gt;

&lt;p&gt;To make it more clear let's look at a real-life example:&lt;/p&gt;

&lt;p&gt;Imagine you have a class for representing employees at a company. Each employee has a name and a salary, and you want to create a method for calculating the monthly paycheck for an employee. Here us how you might define class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
  constructor(name, salary) {
    this.name = name;
    this.salary = salary;
  }

  calculatePaycheck() {
    return this.salary / 12;
  }
}

const tony = new Employee("Tony", 80000);
const ezekiel = new Employee("Ezekiel", 97000);

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

&lt;/div&gt;



&lt;p&gt;Now, suppose you want to create a function that calculates the total payroll for all the employees at the company. To do this, you can use the "bind" method to bind each employee object to the "calculatePaycheck" function, so that the correct salary is used for each employee.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function calculateTotalPayroll(employees) {
  let totalPayroll = 0;
  for (const employee of employees) {
    totalPayroll += employee.calculatePaycheck.bind(employee)();
  }
  return totalPayroll;
}

const employees = [tony, ezekiel];
const totalPayroll = calculateTotalPayroll(employees);


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

&lt;/div&gt;



&lt;p&gt;In this example, the "calculateTotalPayroll" function accepts an array of employee objects as an argument. It then iterates over the array, binding each employee object to the "calculatePaycheck" function using the "bind" method, and calling the resulting function to calculate the monthly paycheck for each employee. Finally, it adds up all the paychecks and returns the total payroll for the company.&lt;/p&gt;

&lt;p&gt;By using the "bind" in this way, you ensure that the correct salary use used for each employee when calculating their paychecks, even if you don't have direct acess to the employee objects within the "calculateTotalPayroll' function.&lt;/p&gt;

&lt;p&gt;An important thing to note about binding is that it creates a new function, rather than modifying the original function. This means that the original function is not changed in any way, and you can bind the same function to multiple objects if needed.&lt;/p&gt;

&lt;p&gt;To wrap up, binding is an important concept in JavaScript that allows you to set up a connection between an object and a function. The most common way to bind an object to a function is using the "bind" method, which creates a new function that is connected to the original function, but with the specified object bound to "this". Binding can be useful in a variety of situations, such as when you want to use the same function with multiple objects, or when you want to pass an object as an argument to a function that doesn't have direct access to that object.&lt;/p&gt;

&lt;p&gt;I hope this article helps you clarify how binding work in JavaScript. &lt;/p&gt;

&lt;p&gt;If you have any questions or comments, please don't hesitate to invoke them!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the Role of Arrow Functions in JavaScript</title>
      <dc:creator>Huseyin Gumus</dc:creator>
      <pubDate>Wed, 21 Dec 2022 22:20:21 +0000</pubDate>
      <link>https://forem.com/erhaneth/what-are-fat-arrow-functions-354p</link>
      <guid>https://forem.com/erhaneth/what-are-fat-arrow-functions-354p</guid>
      <description>&lt;p&gt;&lt;strong&gt;What are Arrow Functions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Arrow functions, also known as "fat arrow" functions, are a concise way to write function expression in JavaScript. They were introduced in ECMAScript6(ES6) and have become popular choice for writing functions in modern JavaScript code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of Arrow Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The syntax for an Arrow function is quite simple. It consist of the following elemets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An optional list of parameters, surrounded by paranetheses.&lt;/li&gt;
&lt;li&gt;The "fat arrow" symbol (=&amp;gt;).&lt;/li&gt;
&lt;li&gt;The function body, which can be a single expression or a block of statements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of a simple arrow function that takes one parameter and returns its square:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const square = (x) =&amp;gt; x * x;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the function has multiple parameters, they are seperated by commas:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const add = (x,y) =&amp;gt; x + y;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the function has no parameters, the parantheses are left empty:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const greet = () =&amp;gt; console.log("First parantheses didn't have any param");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the function body is a block of statements, it must be enclosed in curly braces and the return statement must be explicitly specified:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const sum = (arr) =&amp;gt; {
   let total = 0;
   for (let i = 0; i &amp;lt; arr.length; i++) {
     total += arr[i];
   }
   return total;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Benefits of Using Arrow Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are several benefists of using arrow functions in your code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They are shorter and more concise than traditional function expressions.&lt;/li&gt;
&lt;li&gt;They do not have their own &lt;code&gt;this&lt;/code&gt; value, so they are less likely to have problems with &lt;code&gt;this&lt;/code&gt; binding issues.&lt;/li&gt;
&lt;li&gt;They are well-suited for use in higher-order functions, such as &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(x =&amp;gt; x * 2);
// doubled is [2, 4, 6, 8, 10]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Examples of Using Arrow Functions in Real Applications&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are some examples of how arrow functions are used in real applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a React component, you can use an arrow functions as the event handler for a button click:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyComponent extends React.Component {
  handleClick = () =&amp;gt; {
  // do something when the button is clicked
  }

  render() {
    return (
      &amp;lt;button onClick={this.handleClick}&amp;gt;Click Me!&amp;lt;/button&amp;gt;
    );
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In Node.js server, you can use an arrow function as the callback for asynchronous function:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; fs.readFile('file.text', (error, data) =&amp;gt; {
   if(error) throw error;
   console.log(data);

)};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In a Redux action creator, you can use an arrow function to return an action object:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addTodo = (text) =&amp;gt; ({
  type: ADD_TODO,
  text
)};

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Arrow functions are a useful and concise way to write function in JavaScript. They offer several benefits, including shorter syntax, better handling of the &lt;code&gt;this&lt;/code&gt; value, and ease of use in higher-order functions. While the may not be suitable for every situtation, they are a valuable tool to have in your JavaScript toolbox.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>What is prototypes and how it works in JavaScript ?</title>
      <dc:creator>Huseyin Gumus</dc:creator>
      <pubDate>Thu, 15 Dec 2022 19:53:20 +0000</pubDate>
      <link>https://forem.com/erhaneth/what-is-prototypes-and-how-it-works-in-javascript--32hk</link>
      <guid>https://forem.com/erhaneth/what-is-prototypes-and-how-it-works-in-javascript--32hk</guid>
      <description>&lt;h2&gt;
  
  
  What is prototypes
&lt;/h2&gt;

&lt;p&gt;In JavaScript, a prototype is an object that is associated with every function and object created with a constructor function. &lt;/p&gt;

&lt;p&gt;When a property or method is called on an object, the JavaScript engine looks for the property or method on the object itself. If it is not found, the engine looks for the property or method on the object's prototype. This allows objects to inherit properties and methods from their prototypes, which can save time and code by avoiding the need to define the same properties and methods on multiple objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sounds little complicated right🧐! Let's try explain prototypes in a way that's easy to understand!
&lt;/h2&gt;

&lt;p&gt;A prototype is like a blueprint or a model for creating objects. When you make a new object, you can use the prototype to make sure that the object has all the right properties and methods.&lt;/p&gt;

&lt;p&gt;For example, let's say you want to make a bunch of toy cars. You can create a prototype for a toy car that has all the right parts, like wheels and a steering wheel. Then, when you make a new toy car, you can just copy the parts from the prototype to make sure your toy car has everything it needs. This way, you don't have to make every single toy car from scratch, which would be time-consuming and difficult.&lt;/p&gt;

&lt;p&gt;Prototypes are a way to save time and make sure that your objects have everything they need. In JavaScript, you can use the prototype property of a function to create a prototype for your objects, and then use the new operator to make new objects based on that prototype.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here is an example of how prototypes work in JavaScript, with some explanation:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HUI1eZi6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zdufgafwr9minzapyb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HUI1eZi6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zdufgafwr9minzapyb3.png" alt="Image description" width="677" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, we first define a &lt;strong&gt;Person constructor function&lt;/strong&gt;  that takes a &lt;strong&gt;name argument&lt;/strong&gt; and assigns it to the &lt;strong&gt;name property&lt;/strong&gt; of the object being constructed. This allows us to create multiple &lt;strong&gt;Person objects&lt;/strong&gt; with different &lt;strong&gt;name properties&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next, we add a &lt;strong&gt;sayHello method&lt;/strong&gt; to the &lt;strong&gt;Person prototype&lt;/strong&gt;. This method logs a greeting to the console using the &lt;strong&gt;name property&lt;/strong&gt; of the object on which the method was called. Because the &lt;strong&gt;sayHello method&lt;/strong&gt; is defined on the &lt;strong&gt;Person prototype&lt;/strong&gt;, all &lt;strong&gt;Person objects&lt;/strong&gt; will be able to inherit this method and use it.&lt;/p&gt;

&lt;p&gt;Then, we create two new &lt;strong&gt;Person objects&lt;/strong&gt; using the &lt;strong&gt;Person constructor function&lt;/strong&gt; and the new operator. These objects, &lt;strong&gt;person1&lt;/strong&gt; and &lt;strong&gt;person2&lt;/strong&gt;, each have their own &lt;strong&gt;name property&lt;/strong&gt;, but they do not have a &lt;strong&gt;sayHello method&lt;/strong&gt; defined on them directly.&lt;/p&gt;

&lt;p&gt;However, when we call the &lt;strong&gt;sayHello method&lt;/strong&gt; on each object, the JavaScript engine looks for the method on the object itself and, not finding it, looks for the method on the object's prototype, &lt;strong&gt;Person.prototype&lt;/strong&gt;. The &lt;strong&gt;sayHello method&lt;/strong&gt; is found on the prototype, so it is called and the greeting is logged to the console.&lt;/p&gt;

&lt;p&gt;In this example, the &lt;strong&gt;Person objects&lt;/strong&gt; were able to inherit the &lt;strong&gt;sayHello method&lt;/strong&gt; from their prototype, &lt;strong&gt;Person.prototype&lt;/strong&gt;, thanks to the prototype chain in JavaScript. This can save time and code by avoiding the need to define the same method on each object individually.&lt;/p&gt;

&lt;p&gt;In short, classes are a way to create multiple instances with their own properties, while prototypes provide a way to share properties and methods across multiple objects.&lt;/p&gt;

&lt;p&gt;I hope that helps explain prototypes in JavaScript! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
