Borrowing Methods in JavaScript

Borrowing Methods in JavaScript

Borrowing Methods in JavaScript. By using call(), apply() and bind(), we can easily borrow methods from different objects without having to inherit from them – a useful tool in a professional JavaScript developer’s toolbox.

In JavaScript, sometimes it’s desirable to reuse a function or method on a different object other than the object or prototype it was defined on. By using call(), apply() and bind(), we can easily borrow methods from different objects without having to inherit from them – a useful tool in a professional JavaScript developer’s toolbox.

Methods from native prototypes

In JavaScript, almost everything you touch is an object except for primitives such as string, number and booleans which are immutable. An Array is a type of object suited to traversing and mutating an ordered list of data, and comes with useful methods on its prototype such as slice, join, push and pop.

A common use case we see with objects is to “borrow” methods from an array as they are both list type data structures. The most common borrowed method is Array.prototype.slice.

function myFunc() {

    // error, arguments is an array like object, not a real array
    arguments.sort();

    // "borrow" the Array method slice from its prototype, which takes an array like object (key:value)
    // and returns a real array
    var args = Array.prototype.slice.call(arguments);

    // args is now a real Array, so can use the sort() method from Array
    args.sort();

}

myFunc('bananas', 'cherries', 'apples');

Borrowing methods is possible due to call and apply allowing us to invoke functions in a different context and is a great way to reuse existing functionality without having to make one object extend from another. An array actually has many methods defined on its prototype that are considered generically reusable, two further examples of which are join and filter:

// takes a string "abc" and produces "a|b|c
Array.prototype.join.call('abc', '|');

// takes a string and removes all non vowels
Array.prototype.filter.call('abcdefghijk', function(val) {
    return ['a', 'e', 'i', 'o', 'u'].indexOf(val) !== -1;
}).join('');

As you can see it’s not just objects who benefit by borrowing methods from an array, strings can too. However, since generic methods are defined on the prototype having to write String.prototype or Array.prototype each time we want to borrow a method is verbose and fast becomes tiresome. An alternative, valid approach that is effectively the same is to use Literals.

javascript web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Web Design and Development Services

Get Best out from Web Design and Development Services from Vinew Technologies,We have a dedicated team of experienced and knowledgeable web developers, designers and testers. Therefore, we have proficiency in analyzing, developing and designing the necessity of intricate Website Development projects.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Progressive Web App Development

Hire Full Stack Developer from HireFullStackDeveloperIndia.com to build user-centric & robust Progress Web App Development Solutions in India.