
Functional inheritance and Static methods in JavaScript

In this article, we will see the primary and most usable concepts in Javascript. Yes, it will be Functional inheritance and Static methods.
Functional inheritance
We all know that Javascript is the most famous and confusing language in the world because of its dual support of Object Oriented Programming and Functional Oriented Programming.
Even though it supports OOPS, but it is an optional, mostly we will use Functional Oriented way to write the code. In this functional programming, inheritance plays an inevitable role.
Let's see its impacts.
What is inheritance and why do we need this?
In simple words, inheritance is a property by which a child function can borrow the properties, and variables from its parent function.
Why do we need a Parent and Child function?..why not a single function?
To understand this, we can take the example of attender forms in hospitals.
When we go to the hospital, first we will fill out the attender forms which have allour basic details such as name, age, address, BP and description of our disease.
This is the first and last time, we will fill these details. After that hospital management got this form, they will inherit/borrow the details/properties for further forms/documents/reports such as test reports, prescriptions, etc.
In this way, we can optimize the time and effort to fill the repeated details, also it will eliminate inadvertent errors too.
Hope you understand the concept and purpose of inheritance.
Now, we are going to see how to implement inheritance in Functions.
"use strict"
function laptop (value){ // Parent function
var hp = {} // created a new object hp
hp.model=value.model
hp.year_of_manufacturing=value.year_of_manufacturing // created an attribute called year_of manufacturing inside the object hpreturn hp // this function will return whole object along with its attributes
}
function HP (value){ // child functionvar hp = laptop(value) // The values were inherited from parent function to child function
hp.greet = function(){
return `The laptop model is ${hp.model} and year of manufacturing is ${hp.year_of_manufacturing}`
}
return hp // the object hp will be the output of the child function
}
let HP_pavilion = new HP({model:"pavilion-X50",year_of_manufacturing:2020})
console.log(HP_pavilion.greet()) // now the object can able to use the function greet and able to return its objects and attributes.
/* the output would be
The laptop model is pavilion-X50 and year of manufacturing is 2020
*/
For this. we are going to create two functions namely Parent and Child functions.
Parent function would be laptop and the child function would be HP.
In the Parent function, we will execute the following steps.
- New object “hp” Creation
- Attributes model and year_of_manufacturing creation inside the object hp.
- Return the object hp along with its attributes
In the Child function, we will execute the following steps.
- Object hp creation and initialized with laptop(value) in order to inherit the properties from its parent function.
- The method called greet was created inside the object and now it can able to use object and attributes which are inherited from the parent function laptop
- Return the object hp along with its attributes
Now, we can create the object by assigning it to the child function by passing those two arguments.
Now, those two arguments passed to the child function (greet) which already inherits the variables/properties from the parent class, will return those values(pavilion-X50,2020).
In this way, we can use the inheritance property to get the properties from parent functions and use it for further operations in the child class.
Next, we will going to see Static methods.
Static methods
Static methods are one of the concepts used in Object Oriented Programming. Static methods are used to create objects that contain default information. It would be static, and doesn't have much changes.
We will define the static method by using static keyword.
It will be used when we want to create an object directly from the Class, not from an instance of the class.
Now, we are going to see how to implement static methods in Class.
class Person {constructor(name){
this.name=name
}
get_value(){
return `${this.name}`
}
static get_post(post){
let name = post == "ChangpengZhao" ? "He is a CEO" : "He is not a CEO"
return new Person(name)
}
}let who_is_ceo = Person.get_post("ChangpengZhao")
console.log(who_is_ceo.name)The above example is used to print whether the given name belongs to the name of the CEO or not.
The first step is to create the class, then create the constructor to assign and store the values in “this” memory location.
get_value() function is used to get the value since we can't directly access the value from the constructor from outside.
Now, create the static function with static keyword and implement the ternary operator so that if we give the correct CEO name, it will return He is a CEO , otherwise it will return He is not a CEO.
let object = Class.<static-function>("ChangpengZhao")Then use the above syntax for calling the static function. We have to use Class directly or else it will throw the errors.
Generally, Static methods are used across the instance of the class, so it should be bound to the class.
In this way, we can implement the static methods in JavaScript.
Thats it.
Thank you all for your reads.
Kindly refer to this link for the code.
Written by
Sri Vishnuvardhan
Building production-grade geospatial platforms that combine GIS, remote sensing, cloud infrastructure, AI, and platform engineering to transform spatial data into scalable, real-world intelligence.
Enjoyed this article?
Subscribe to get notified when new articles on DevOps, Cloud, and platform engineering are published.
You'll receive a welcome email with a confirmation link. No spam — unsubscribe anytime.
Have a project in mind?
I work on cloud-native platforms, DevOps automation, and geospatial intelligence systems. Let's talk.