Logo


Method Chaining


Method chaining is a programming strategy that simplifies your code and beautifies it. Method chaining is done by ensuring that each method on an object returns the entire object, instead of returning a single element of that object. For example:


1function door(){
2    this.height="";
3    this.width="";
4    this.status="close";
5}
6door.prototype.Open=function ()
7{
8    this.status="Open"
9    return this
10}
11door.prototype.Closed=function ()
12{
13    this.status="closed"
14    return this
15}
16door.prototype.setProperty=function (width,height)
17{
18    this.width = width;
19 this.height = height;
20 return this;
21
22}
23door.prototype.doorStatus=function ()
24{
25    console.log('The',this.width,'x',this.height,'Door is',this.status);
26 return this;
27}
28
29var smallDoor=new door()
30smallDoor.setProperty(20,40).Open().doorStatus().Closed().doorStatus();
31


Note that each method in Door.prototype returns this, which refers to the entire instance of that Door object.

About

Moiz is a software engineer from Arid University with a passion for writing tech tutorials and doing coding. I am continously produce JavaScript and other web development technology tutorials in concepts through easy-to-understand explanations written in plain English.I have expertise in next js ,react js ,javascript,html,bootstrap and node js.

Follow him on Twitter

Hassan is a software engineer from kohat university of science and technology with a passion for writing tech tutorials and doing javascript practices on daily basis.I have expertise in next js ,react js ,javascript,html,bootstrap and node js. Problem Solving and making concpets is my strong point.

Follow him on Twitter

Tags

Click to see all tutorials tagged with:

© Copyright 2023 Pak Annonymous | Back To Homepage | Privacy Policy
Share