Follow @endyourif rss Facebook LinkedIn

Add Function to Prototype with Javascript Add Function to Prototype with Javascript

Published on Jun 26, 2022

In some of my more recent posts about How to compare arrays in Javascript and Format Numbers as Currency with Javascript I have been enjoying adding them as extension methods to simplify their usage.

Honestly, I've always been afraid to do it because it seems "hard". Now that I've done it once though, it is now becoming second nature. I really can't believe I haven't done this before because I absolutely love adding string extensions when I developer with C#.

Let's take a look at some code to add a function with prototype using Javascript. In this first example I will add it to the Javascript Array Prototype:


// Extend the Array property
Array.prototype.equals = function (array2) {
if (!this || !array2)
return false;
if (this.length !== array2.length)
return false;
var array1Sorted = this.slice().sort();
var array2Sorted = array2.slice().sort();
return array1Sorted.every(function(value, index) {
return value === array2Sorted[index];
});
}

The concept for Number is the exact same, you simply replace Array with Number as in the following example. I could see this being used in conjunction with the JavaScript String concat() Method


// Extend the number property
Number.prototype.currency = function(locale, isoCode) {
var formatter = new Intl.NumberFormat(locale, { style: 'currency', currency: isoCode });
return formatter.format(this);
}

Hopefully with these two examples this will now become second nature to your Javascript code as it is quickly becoming mine!

Tags: JavaScript | jQuery Tutorial

About Jamie Munro

Welcome to my blog. I have been compiling articles since 2009! Below is a list of every post I've ever created. Some of my major focuses have been compiling the best source of jQuery tutorials on the Internet today. A recent focus of mine is now focusing on creating the best SQL Tutorials for Beginners, Intermediate and Advanced Users. This is a compiliation of all my experience about how to do common SQL commands to optimize for performance. As you might see from the links around here, I've also written and published 5 books. My first and last book are focused around ASP.NET MVC and Web API tutorials. The last book has a big focus on integrating Knockout.js as the MVVM for the front-end code. O'Reilly Media was so interested in Knockout.js that they actually commissioned me to write that book first as it is one of the only books dedicated entirely to this MVVM library. So if you're looking for the best Knockout.js tutorials, I've compiled the best list. My first book, even though it was self published, was on the PHP framework: CakePHP. I used this framework for years and many of my first ever exampes were all focused on CakePHP tutorials that provide so many fantastic PHP examples. There are so many Javascript tutorials out there, but I noticed that one of the things I work on most with Javascript is arrays. With this I've worked hard on compiling a big list of really advanced Javascript array examples. These array examples are amazing including how to group by, get distinct array elements and so many more. I've also dabbled a little bit with Node.js and during those experiments I definitely ran into some common errors like Can't set headers after they are sent, Solving No Access-Control-Allow-Origin with Node js and Express, and Uncaught ReferenceError: require is not defined. With these common errors I've compiled a list of Node.js tutorials that help solve these problems, but more importantly getting you started with the basics.