Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to add method to String class in JavaScript ?

How to add method to String class in JavaScript ?

In this article, the task is to add a method to the String class in JavaScript. There are two approaches that are described with the proper examples: 

Approaches to add Methods to String Class:

Approach 1: Using Object.defineProperty() method

The Object.defineProperty() method is used to define a new property directly to an object or modify an existing property. It takes 3 arguments, the first is Object, the Second is propertyName, and the last is propertyDescription. In this example, the sum of the length of strings is returned.

Syntax: 

Object.defineProperties(obj, props) 

Example: This example uses the above-explained approach.

Javascript




// Input string
let str1 = "neveropen";
let str2 = "A Computer Science Portal";
 
// Display input string
console.log(str1);
console.log(str2);
 
 
// Define custom method
Object.defineProperty(String.prototype, "SumOfLength", {
    value: function (param) {
        return this.length + param.length;
    },
});
 
// Run custom method
function GFG_Fun() {
    // Apply custom method
    let res = str1.SumOfLength(str2);
 
    // Display output
    console.log("Total length: " + res);
}
 
// Funcion call
GFG_Fun();


Output

neveropen
A Computer Science Portal
Total length: 38

Approach 2: Using String.prototype.propertyName method

The String.prototype.propertyName is used to add a new method to the String class. Define a method that takes arguments passed by the object and performs the desired operation. In this example, the sum of the length of strings is returned.

Syntax:

object.prototype.name = value

Example: This example uses the above-explained approach.

Javascript




// Input string
let str1 = "JavaScript";
let str2 = "A Scripting Language for Web";
 
// Display input string
console.log(str1);
console.log(str2);
 
// Define custom method
String.prototype.SumOfLength = function (arg) {
    return this.length + arg.length;
};
 
// Run custom method
function GFG_Fun() {
    // Apply custom method
    let res = str1.SumOfLength(str2);
 
    // Display output
    console.log("Total length: " + res);
}
 
// Funcion call
GFG_Fun();


Output

JavaScript
A Scripting Language for Web
Total length: 38

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments