
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Function borrowing in JavaScript.
The call(), apply() and bind() are used to borrow methods in JavaScript.
Following is the code for borrowing methods in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result,.sample { font-size: 18px; font-weight: 500; color: rebeccapurple; } .result { color: red; } </style> </head> <body> <h1>Borrowing a method in JavaScript</h1> <div class="sample"></div> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to borrow the welcome method of object obj</h3> <script> let sampleEle = document.querySelector(".sample"); let resultEle = document.querySelector(".result"); let obj = { firstName: "Rohan", lastName: "Sharma", welcome() { return "Welcome " + this.firstName + " " + this.lastName; }, }; let obj1 = { firstName: "John", lastName: "Shaw", }; sampleEle.innerHTML = "obj.welcome() = " + obj.welcome(); document.querySelector(".Btn").addEventListener("click", () => { resultEle.innerHTML = "obj1.welcome() = " + obj.welcome.call(obj1); }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Function returning another function in JavaScript
- Function Expression vs Function Declaration in JavaScript?
- ArrayBuffer.isView() function in JavaScript
- ArrayBuffer.slice() function in JavaScript
- Atomics.add() function in JavaScript
- Atomics.and() function in JavaScript
- Atomics.or() function in JavaScript
- Atomics.isLockFree() function in JavaScript
- Atomics.load() function in JavaScript
- Atomics.store() function in JavaScript
- Atomics.sub() function in JavaScript
- Atomics.xor() function in JavaScript
- DataView.getFloat32() function in JavaScript
- DataView.getFloat64() function in JavaScript
- DataView.getInt16() function in JavaScript

Advertisements