<?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>DEV Community: Sachin Koli</title>
    <description>The latest articles on DEV Community by Sachin Koli (@sachinkoli).</description>
    <link>https://dev.to/sachinkoli</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%2F483818%2F5440d856-d620-4981-9f0a-e92d348270c1.jpg</url>
      <title>DEV Community: Sachin Koli</title>
      <link>https://dev.to/sachinkoli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sachinkoli"/>
    <language>en</language>
    <item>
      <title>call(), apply() and bind() in JavaScript explained</title>
      <dc:creator>Sachin Koli</dc:creator>
      <pubDate>Sat, 10 Oct 2020 10:21:10 +0000</pubDate>
      <link>https://dev.to/sachinkoli/call-apply-and-bind-in-javascript-explained-4kk2</link>
      <guid>https://dev.to/sachinkoli/call-apply-and-bind-in-javascript-explained-4kk2</guid>
      <description>&lt;p&gt;In my last post, I discussed the Function constructor. How the function object created from it and the properties and methods of the function object.&lt;/p&gt;

&lt;p&gt;In this article, we will go into the details of the following three function methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;call()&lt;/li&gt;
&lt;li&gt;apply()&lt;/li&gt;
&lt;li&gt;bind() &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are basically used to call the function (except bind, bind() returns a new function that we can use as per our need). They all take a &lt;code&gt;this&lt;/code&gt; value depending upon the context to execute the function in that context. Let's take a look at each one in detail. &lt;/p&gt;




&lt;h1&gt;
  
  
  call()
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" rel="noopener noreferrer"&gt;MDN definition&lt;/a&gt; : The call() method calls a function with a given &lt;code&gt;this&lt;/code&gt; value and arguments provided individually.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" rel="noopener noreferrer"&gt;Syntax&lt;/a&gt;&lt;/strong&gt;: func.call([thisArg[, arg1, arg2, ...argN]])&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;thisArg&lt;/strong&gt;: optional (this will be the value upon which the function would call)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;arg1&lt;/strong&gt;, &lt;strong&gt;arg2&lt;/strong&gt;, &lt;strong&gt;...argN&lt;/strong&gt;: optional (arguments of the function)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;the first argument represents a value on which the function would call (it refers to the current object/the calling object)&lt;/li&gt;
&lt;li&gt;other arguments represent the value to the parameter of the function&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;Let's take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// defining a global variable
var lastName = 'global_name';

const func = function(firstName) {
    return firstName + " " + this.lastName; /** the value of 'this' 
    is defined how we call the function */
}

// this object is passed as the first argument to the call method
var person = {
    lastName: 'person_name'
}

// calling the function usually
func('Sachin'); // Sachin global_name

/** calling the function using the call method and setting the 
'this' value to the 'person' object */
func.call(person, 'Sachin'); // Sachin person_name

// using call method without passing the first argument
func.call(); // undefined global_name

// passing the first argument as null or undefined
func.call(null, 'Sachin'); // Sachin global_name
func.call(undefined, 'Sachin'); // Sachin global_name

/******************** in strict mode*****************/
func.call(); /** Cannot read property 'lastName' of undefined*/
func.call(null, 'Sachin'); /** Cannot read property 'lastName' of null*/
func.call(undefined, 'Sachin'); /** Cannot read property 
'lastName' of undefined*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As seen from the example, we can use the call method to call a function on any object. &lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When usually calling the function then &lt;code&gt;this&lt;/code&gt; value will set to the global object &lt;code&gt;window&lt;/code&gt;. This &lt;code&gt;window&lt;/code&gt; object is having a property &lt;code&gt;lastName&lt;/code&gt; which we defined globally in our code will return from the function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When calling the function using the call method and passing the first argument a &lt;code&gt;person&lt;/code&gt; object then &lt;code&gt;this&lt;/code&gt; value will set to that &lt;code&gt;person&lt;/code&gt; object (not &lt;code&gt;window&lt;/code&gt; object this time) and its &lt;code&gt;lastName&lt;/code&gt; property will return.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the call method without passing any arguments, &lt;code&gt;this&lt;/code&gt; value will set to the global object &lt;code&gt;window&lt;/code&gt; and its property &lt;code&gt;lastName&lt;/code&gt; will return.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the first argument passed is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; then still the &lt;code&gt;this&lt;/code&gt; will set to the global &lt;code&gt;window&lt;/code&gt; object in this case.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Caution: For strict mode
&lt;/h4&gt;

&lt;p&gt;In 'strict mode', the value of &lt;code&gt;this&lt;/code&gt; will be &lt;code&gt;undefined&lt;/code&gt;. To know about strict mode refer to this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  apply()
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply" rel="noopener noreferrer"&gt;Syntax&lt;/a&gt;&lt;/strong&gt;: func.apply(thisArg, [ argsArray])&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;thisArg&lt;/strong&gt;: (this will be the value upon which the function would call)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;argsArray&lt;/strong&gt;: optional (arguments of the function passed in an array)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;apply() is almost similar to call() except that it takes an array as a second argument and passes the members of that array as arguments to the calling function.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name = 'Sachin';

const func = function (age, hobby) {
  return (this.name + ' is ' + age + ' years old and his hobby is '
  + hobby);
};

var person = {
    name: 'John'
}

func(); /** Sachin is undefined years old and his 
hobby is undefined*/
func.apply(); /** Sachin is undefined years old and his 
hobby is undefined*/

console.log(func() === func.apply()); /** true*/

func('15', 'writing'); /** Sachin is 15 years old and his 
hobby is writing*/
func.apply(undefined, ['15', 'writing']); /** Sachin is 15 years 
old and his hobby is writing*/
func.apply(null, ['15', 'writing']); /** Sachin is 15 years 
old and his hobby is writing*/

/********* changing 'this' to 'person' object*********/
func.apply(person, ['20', 'music']); /** John is 20 years 
old and his hobby is music*/

/**************** strict mode ***************/
/** Cannot read property 'name' of undefined*/
func(); 
func('15', 'writing'); 
func.apply();
func.apply(undefined, ['15', 'writing']);

/** Cannot read property 'name' of null */
func.apply(null, ['15', 'writing']); 

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

&lt;/div&gt;






&lt;h1&gt;
  
  
  bind()
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind" rel="noopener noreferrer"&gt;Syntax&lt;/a&gt;&lt;/strong&gt;: func.bind(thisArg[, arg1[, arg2[, ...argN]]])&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;bind() method creates and returns a copy of the function &lt;code&gt;func&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;when that new function is called, it has it's &lt;code&gt;this&lt;/code&gt; value set to the value provided by &lt;code&gt;thisArg&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;arg1, arg2,..., argN are arguments that prepends to the arguments of that new returned function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand this with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// defining a person object
/** this object has a property 'age' and a method 
'getNameAndAge' */
const person = {
    age: 42,
    getNameAndAge: function(name) {
        return name + ' ' + this.age;
    }
}

// calling the method on the 'person' object directly
person.getNameAndAge('Sachin'); // Sachin 42

// assigning the reference of the method to variable nameAndAge
const nameAndAge = person.getNameAndAge;

// calling the function assigned to nameAndAge by referencing it 
nameAndAge('Sachin'); /** Sachin undefined (the function gets
invoked at the global scope)*/

// use of bind method
const boundNameAndAge = nameAndAge.bind(person, 'Sachin');
boundNameAndAge() /** Sachin 42 (bind method creates
a new function and bounds 'this' value to 'person' object)*/

// bind without any arguments
const boundNameAndAge = nameAndAge.bind();
boundNameAndAge('Sachin') // Sachin undefined

// setting 'this' to 'undefined'
const boundNameAndAge = nameAndAge.bind(undefined, 'Sachin'); 
boundNameAndAge() // Sachin undefined

// setting 'this' to 'null'
const boundNameAndAge = nameAndAge.bind(null, 'Sachin'); 
boundNameAndAge() // Sachin undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When we are executing &lt;code&gt;nameAndAge('Sachin');&lt;/code&gt;, we are executing that function in the global scope and &lt;code&gt;this&lt;/code&gt; here refers to the global &lt;code&gt;window&lt;/code&gt; object and we have not defined &lt;code&gt;age&lt;/code&gt; in the global scope, that's why it returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;const boundNameAndAge = nameAndAge.bind(person, 'Sachin');&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;bind&lt;/code&gt; method creates and returns a copy of nameAndAge function and sets &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;person&lt;/code&gt; object. We are assigning that newly created function to variable &lt;code&gt;boundNameAndAge&lt;/code&gt;. When we execute &lt;code&gt;boundNameAndAge()&lt;/code&gt;, has it's &lt;code&gt;this&lt;/code&gt; set to &lt;code&gt;person&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; property of &lt;code&gt;person&lt;/code&gt; object returns.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;In case of no arguments or &lt;code&gt;this&lt;/code&gt; set to &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, the &lt;code&gt;this&lt;/code&gt; value for the newly created function is decided by the &lt;code&gt;this&lt;/code&gt; of the executing scope.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;call() and apply() executes the function immediately, whereas bind() returns a new function.&lt;/li&gt;
&lt;li&gt;the object/value on which the function executes depends on the &lt;code&gt;this&lt;/code&gt; value defined by the context.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for reading. If you found this article helpful please like and share it with others so that they will also get the benefit. Feedbacks are welcome: &lt;a href="https://twitter.com/toocoolsachin8" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; | &lt;a href="https://instagram.com/toocoolsachin" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt; | &lt;a href="https://linkedin.com/in/sachin-koli" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>What actually is a "function" in JavaScript?</title>
      <dc:creator>Sachin Koli</dc:creator>
      <pubDate>Tue, 06 Oct 2020 17:22:28 +0000</pubDate>
      <link>https://dev.to/sachinkoli/what-actually-is-a-function-in-javascript-ljm</link>
      <guid>https://dev.to/sachinkoli/what-actually-is-a-function-in-javascript-ljm</guid>
      <description>&lt;p&gt;In JavaScript, the functions are &lt;strong&gt;first-class objects&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Now, one would ask, what is a first-class object? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;: Just like any other object in JavaScript, functions are also objects. They can have properties and methods associated with them. But they differ from other objects in the sense that they are &lt;a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function" rel="noopener noreferrer"&gt;Function&lt;/a&gt; objects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Focus your attention on this keyword: &lt;strong&gt;Function&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Function&lt;/strong&gt; is a constructor to make an instance of a function. Or in other words, a Function constructor is used to make a function object. This means that just like in any other programming language we call the constructor of a class to initiate an instance (object) of that class, similarly, here calling the Function constructor can create functions dynamically.&lt;/p&gt;

&lt;p&gt;Just like an object has properties and methods, the functions in JavaScript can also have properties and methods associated with them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function properties:
&lt;/h3&gt;

&lt;p&gt;There are various properties of function like arguments, length, name, etc. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" rel="noopener noreferrer"&gt;More details...&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Function methods:
&lt;/h3&gt;

&lt;p&gt;There are various methods like call(), apply(), bind(), etc. I will post a different article about these methods in detail later. If you want to check them out now &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" rel="noopener noreferrer"&gt;click here.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function" rel="noopener noreferrer"&gt;syntax&lt;/a&gt; of calling the Function constructor:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;new Function([arg1 [, arg2 [, ...argN]] ,] functionBody)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;arg1, arg2, ...argN&lt;/em&gt;&lt;/strong&gt;: Corresponds to the arguments passed to the function expression. Each must be a string or list of strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;functionBody&lt;/em&gt;&lt;/strong&gt;: JavaScript statements inside the function body. Must be a string.&lt;/p&gt;

&lt;p&gt;Now let's have a look at an example of calling the Function constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Creates a function that takes an argument of name, and returns the concatenated string containing the argument passed 
const greet = new Function('name', 'return "Hello " + name');

// Call the function
greet('John'); // Hello John

// Calling without argument
greet(); // Hello undefined

// It is equivalent to defining a function expression like this
const greet = function(name) {
    return 'Hello ' + name;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the arguments should be strings. All arguments passed to the Function constructor are treated as the names of the parameters in the function created. Omitting an argument while calling the function can result in an undefined value for that parameter.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for reading! If you found this article helpful please share it with others so that they will also get the benefit. &lt;br&gt;
Feedbacks are welcome:  &lt;a href="https://twitter.com/toocoolsachin8" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; | &lt;a href="https://instagram.com/toocoolsachin" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt; | &lt;a href="https://linkedin.com/in/sachin-koli" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

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