Prototyping in JavaScript

Prototyping in JavaScript

The prototype property is an object (called as prototype object) has a constructor property by default. constructor property points back to the function on which the prototype object is a property. We can access the function’s prototype property using the syntax functionName.prototype.
I usually loved to work on Python has many inbuilt methods like the title.
While working on the front-end part I prefer to use JQuery or Plain Vanilla javascript. I found some lack of method like the title. So I decided to build a method which does the same action as the title in python.
First, I build a simple method which takes a string as an argument which makes the title for that string.
After 5-6 months working in front-end technology, I came to know about Prototyping in Javascript.

Prototype for the title the string.
String.prototype.title = function() {
    if (this == "None" || this == "") {
        return "-"
    }
    var string = this.toLowerCase()
    string = string.replace(/(^|\s)[a-z]/g,function(f){return f.toUpperCase();});
    return string.replace(/_/g, " ").replace(/-/g, " ")
}
Prototype for the INR Symbol.
Number.prototype.getINR = function() {
    return this.toLocaleString('en-IN',{
        maximumFractionDigits: 2,
        style: 'currency',
        currency: 'INR'})
}
usage: let amount = 50
           amount.getINR()
       console.log(amount)
       > '₹ 50.0'

Comments

Popular Posts