GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Array Object
  • JS Boolean Object
  • JS Date Object
  • JS Math Object
  • JS Number Object
  • JS String Object
  • JS Reserved Keywords
  • More references

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialrepublic.com — JavaScript Borrowing Methods - Tutorial Republic

TutorialRepublic
HOME HTML5 CSS3 JAVASCRIPT JQUERY BOOTSTRAP5 v4.6 PHP7 SQL REFERENCES EXAMPLES FAQ SNIPPETS Online HTML Editor
JAVASCRIPT BASIC
JS Introduction JS Getting Started JS Syntax JS Variables JS Generating Output JS Data Types JS Operators JS Events JS Strings JS Numbers JS If…Else JS Switch…Case JS Arrays JS Sorting Arrays JS Loops JS Functions JS Objects
JAVASCRIPT & DOM
JS DOM Nodes JS DOM Selectors JS DOM Styling JS DOM Get Set Attributes JS DOM Manipulation JS DOM Navigation
JAVASCRIPT & BOM
JS Window JS Screen JS Location JS History JS Navigator JS Dialog Boxes JS Timers
JAVASCRIPT ADVANCED
JS Date and Time JS Math Operations JS Type Conversions JS Event Listeners JS Event Propagation JS Borrowing Methods JS Hoisting Behavior JS Closures JS Strict Mode JS JSON Parsing JS Error Handling JS Regular Expressions JS Form Validation JS Cookies JS AJAX Requests JS ES6 Features
JAVASCRIPT EXAMPLES
JS Practice Examples JS FAQ's Answers
JAVASCRIPT REFERENCE
  • JS Properties and Methods
    • JS Array Object
    • JS Boolean Object
    • JS Date Object
    • JS Math Object
    • JS Number Object
    • JS String Object
  • JS Reserved Keywords
  • More references
Advertisements

JavaScript Borrowing Methods

In this tutorial you will learn how to borrow functionality from existing objects in JavaScript.

Borrowing Methods from Objects

In JavaScript, you can borrow methods from other objects to build some functionality without inheriting all their properties and methods.

JavaScript provides two methods for all function objects, call() and apply(), that allow a function to be invoked as if it were a method of another object. Here's an example:

Example

Try this code »
let objA = {
    name: "object A",
    say: function(greet) {
        alert(greet + ", " + this.name);
    }
}

objA.say("Hi"); // Displays: Hi, object A

let objB = {
    name: "object B"
}

/* The objB doesn't have say() method, but it can borrow it from objA */
objA.say.call(objB, "Hello"); // Displays: Hello, object B

Difference Between call() and apply() Methods

The syntax of the apply() method is almost identical to call(), the only difference is, the call() method takes a list of arguments like call(thisObj, arg1, arg2, ...), while the apply() method takes a single array of arguments like apply(thisObj, [argsArray]).

Notice the square brackets ([]), which denotes an array, in the last line of the following example:

Example

Try this code »
let objA = {
    name: "object A",
    say: function(greet) {
        alert(greet + ", " + this.name);
    }
}

objA.say("Hi"); // Displays: Hi, object A

let objB = {
    name: "object B"
}

/* The objB doesn't have say() method, but it can borrow it from objA */
objA.say.apply(objB, ["Hello"]); // Displays: Hello, object B

Using Built-in Methods

The apply() method also allows you to use built-in methods for performing some tasks quickly and easily. One such example is using the Math.max()/Math.min() to find out the maximum or minimum value in an array, that would otherwise require looping over the array values.

As you already know from the previous chapters JavaScript arrays do not have a max() method, but Math has, so we can apply the Math.max() method, like this:

Example

Try this code »
let numbers = [2, 5, 6, 4, 3, 7];

// Using Math.max apply
let max = Math.max.apply(null, numbers);
alert(max); // Outputs: 7

Note: The first argument to both call() and apply() is the object on which the function is to be invoked. Using null as the first argument is like calling the function without providing any object for the this pointer inside the function.

The new ES6 spread operator provides a shorter way to obtain the maximum or minimum value from an array without using the apply() method. Here's an example:

Example

Try this code »
let numbers = [2, 5, 6, 4, 3, 7];

// Using spread operator
let max = Math.max(...numbers);
alert(max); // Outputs: 7

However, both spread (...) and apply() will either fail or return the incorrect result if the array has too many elements (e.g. tens of thousands). In that case you can use the Array.reduce() to find the maximum or minimum value in a numeric array, by comparing each value, like this:

Example

Try this code »
let numbers = [2, 5, 6, 4, 3, 7];

// Using reduce method
let max = numbers.reduce(function(a, b) {
    return Math.max(a, b);
});
alert(max); // Outputs: 7
Previous Page Next Page
Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties
Advertisements

Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates.

About Us

Our Story Terms of Use Privacy Policy

Contact

Contact Us Report Error Guest Posts Advertise

Interactive Tools

Bootstrap Icon Search Utility HTML Formatter Title & Meta Length Calculator HTML Color Picker Bootstrap Button Generator SQL Playground Font Awesome Icon Finder HTML Editor

TutorialRepublic

BMC
Copyright © 2026 Tutorial Republic. All Rights Reserved.
Share This:
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.