BIND, CALL and APPLY in JAVASCRIPT

Photo by Shahadat Rahman on Unsplash
Bind() method
var person = {
firstName: 'John',
lastName: 'Wick',
showName: function() {
console.log (this.firstName + '' + this.lastName);
}
};

// showName is passed as callback, here this is the button
$('button').click(person.showName);

// Use bind to define this value
$('button').click(person.showName.bind(person));
// this is still the object person
function log(level, time, message) {
console.log(level + ' �" ' + time + ': ' + message);
}
function log(level, time, message) {
console.log(level + ' - ' + time + ': ' + message);
}

function logErrToday(message) {
log("Error", "Today", message);
}

logErrToday("Server die."); // Error - Today: Server die.
function log (level, time, message) {
console.log (level + '-' + time + ':' + message);
}

// There is no this so this is set to null
// Set default 2 parameters level and time
var logErrToday = log.bind (null, 'Error', 'Today');

// This function corresponds to log ('Error', 'Today', 'Server die.')
logErrToday ("Server die.");
// Error - Today: Server died.

Call and Apply

// Find max by calling Math.max function
Math.max (4, 3, 2, 10);

// Instead of calling Math.max function directly, we can use call
// Set this with null
Math.max.call (null, 4, 3, 2, 10);

// Apply is similar to call, but does not pass in turn
// Which passes an array containing all the parameters
Math.max.apply (null, [4, 3, 2, 10]);
function test(firstParam, secondParam, thirdParam){
var args = Array.apply(null, arguments);
console.log(args);
}

test(1, 2, 3); // [1, 2, 3]
var computer = {
accessWeb: function (site) {
// Go to some site
console.log ('Go to:' + site);
}
};

computer.accessWeb ('abc.com');
var computer = {
accessWeb: function (site) {
// Go to some site
console.log ('Go to:' + site);
}
};

var oldFunction = computer.accessWeb;
// Replace the function accessWeb with the new function
computer.accessWeb = function () {
console.log ('start to web');
oldFunction.apply (this, arguments); // keep the old function
console.log ('entered web');
}

computer.accessWeb ('abc.com');

References

--

--

--

Always be nice to anybody who has access to my toothbrush.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Node.js PostgreSQL CRUD example: Rest APIs with Express & Sequelize

Get list of duplicate objects in an array of objects

3 Common Problems When Writing Unit Tests In Jasmine

AngularJs styled form handling with ReactJs Hooks

Ember.js in 4 Days: How I Got Burned and What I Learned

Journey of ReactJS Component’s Life Cycle

Supabase — The Open-source Firebase Alternative

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beribey

Beribey

Always be nice to anybody who has access to my toothbrush.

More from Medium

JavaScript String and some useful String methods

How to exit/break from a Javascript looping

Use normalize() to duel with non-English string in javascript

How TypeScript works with JavaScript null and undefined values