<?php
//Include config file
include_once("../includes/config.inc.php");

//Set page variables
$page_title = "Method Chaining";
$page_description = "How to use method chaining in Javascript.";
$page_author = "Cody Ostrander";

//Include header
include_once("../includes/header.inc.php");
?>
			<!-- Beginning of blog post -->
	    	<div class="blog-post">
	    		<h2 class="blog-post-title">Method Chaining</h2>
	    	  	<p class="blog-post-meta">September 17 2020 by Cody Ostrander</p>

	    	  	<p>How method chaining works in Javascript.</p>
	    	  	<hr>
	    	  	<h3>What is Method Chaining?</h3>
	    	  	<p>
				  Let's talk about method chaining in Javascript. What is method chaining? Method chaining, also known as cascading, is when you call multiple methods, one after 
				  another on an object. You can avoid method chaining, but you will likely end up with a lot of repitition and unnecessary typing. When using Javascript, you will 
				  frequently come across method chaining. 
	    	  	</p>
	    	  
	    	  	<p>
					  When you call one or methods upon an object multiple times in a row, this is when you could use method chaining. Here is an example of unnecessary repitition 
					  that could have been avoided by using method chaining:
	    	  	</p>
	    	  	<pre class="language-js"><code>
class Radio{

	var volume = 0;

	increaseVolume(){
		this.volume++;
		return this;
	}

	let newCarRadio = new Radio();
	newCarRadio.increaseVolume();
	newCarRadio.increaseVolume();
	newCarRadio.increaseVolume();
	
}
			  	</code></pre>
	    	  	<h3>How to use Method Chaining</h3>
	    	  	<p>
				  Now, how exactly does method chaining work? The key is using "this". "This" refers to the current object instance in Javascript. In the increaseVolume() method, we 
				  return "this". In this case, "this" is a Radio object. Since we are returning a Radio object, we can call another method. We call increaseVolume() again, which 
				  again returns "this" which is a Radio object. As long as we return a Radio object, we can continue to chain methods together. Now let's look at the example 
				  from above again, but this time using method chaining instead of using unnecessary repitition.
				  </p>
				  <pre class="language-js"><code>
class Radio{

	var volume = 0;

	increaseVolume(){
		this.volume++;
		return this;
	}

	let newCarRadio = new Radio();
	newCarRadio.increaseVolume().increaseVolume().increaseVolume();
	
}
				  </code></pre>
				  <p>
					  This one small code sample here saved multiple lines of typing, and you didn't have to type newCarRadio everytime you wanted to call a method on it. It doesn't 
					  seem like much here, but when you look at an entire project's worth of code, you will find that method chaining can save you a lot of time and unnecessary typing. 
					  Next time you are with objects in Javascript, try out method chaining.
				  </p>

	    	</div>
			<!-- End of blog post -->
<?php
include_once("../includes/footer.inc.php");
?>