473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Introduction on JavaScript Apply, Call, and Bind Methods

Being the most popular programming language, JavaScript can be executed on any browser, desktop, or mobile device. The concept of functional programming revolves around the methods following certain rules. These JavaScript methods are usually properties that contain function definition and are performed on the objects. Powerful methods like apply, call and bind are used under the JavaScript when there is need to pass the function as a variant.

Use of bind() Method in JavaScript

The bind() method is a challenging concept when there is a need to deal with events. When started with the JavaScript, function binding is the practice to keep the context of this keyword within another function. The bind() method creates a function similar to the original one where the first argument determines the value of this keyword. This technique is usually required in some methods which are called from the receiver object.
  • The bind() method allows the user to set this value on methods.
  • The bind () method makes it easier to borrow a method either by Apply or by Call method.
  • The bind() method is used as a currying function.
Use of Call() & Apply() Method in JavaScript

Both the methods serve the same purpose as they allow the object to use another object’s method. They invoke the function immediately where bind() method returns the bound function. The only difference in these JS methods is that apply() function runs the function within the array of parameters whereas the call() method requires a list of arguments which is quite similar to the standard function invocation. This means that both the methods can be called on by the functions.
  • Just like bind() method, this value can be invoked using the call and apply function.
  • Call and Apply methods can also borrow the functions just like bind() but in a more versatile manner.
  • Apply() method is useful in creating variadic functions which means a single function can accept a number of arguments rather than fixed value.
Although, Call, apply and bind methods are quite complex to understand but they can become easier when practiced well. If you desire to become a perfect JavaScript coder with solid concepts,practice complex programming codes on any online platform like CodeFights.

The author of this article is a programming enthusiast who basically writes useful content information for JavaScript programmers. Keep watching this space and remain updated with all latest JavaScript approaches.
Mar 21 '18 #1
2 2531
gits
5,390 Expert Mod 4TB
to make a bit more clear what the article handles i am trying to give an example. Usually we need this when handling objects that are created to handle specific things in an application (encapsulation, separation of concern, a design pattern or whatever it is that makes us building objects). since JavaScript allows us to pass functions as parameters to other functions, we usually refer to that as callbacks, we may have an issue with scoping the functions correctly. The mentioned methods in the article help to set the scope correctly. So lets go ahead and create the problem first:

Expand|Select|Wrap|Line Numbers
  1. function OBJ_X() {
  2.     this.foo = 'bar';
  3. }
  4.  
  5. OBJ_X.prototype.method_x = function(param) {
  6.     console.log(this.foo + ' ' + param);
  7. }
  8.  
  9. function OBJ_Y() {
  10. }
  11.  
  12. OBJ_Y.prototype.method_y = function(cb) {
  13.     cb('whatever_param');
  14. }
  15.  
  16. var ox = new OBJ_X;
  17. var oy = new OBJ_Y;
  18.  
  19. oy.method_y(ox.method_x);
What did we do? We did define a very simple class OBJ_X that has a instance variable foo with the value 'bar'. when we call the method_x and a param on an instance of this class we expect something like:

'bar whatever_param'

in the console.

Now we have another simple class OBJ_Y and its method_y expects a parameter - a callback (it could be for example the handler of an async operation or such where we want to invoke the method_x on the instance of OBJ_X with a response or such - i simply pass on a string to the callback as it shows the principle). So simply put - we create an instance of each class and then invoke method_y with method_x as parameter and call method_x from there with a parameter itself - and we get the following output in the console:

'undefined whatever_param'

The actual output is not what we expect - because doing it like this we lose the intended execution context (scope) for method_x - instead the context will now be the main window scope - thus in method_x the reference to this.foo gives us an undefined value. To fix that we can use the 3 methods mentioned above.

(a) using the bind method:
Expand|Select|Wrap|Line Numbers
  1. function OBJ_X() {
  2.     this.foo = 'bar';
  3. }
  4.  
  5. OBJ_X.prototype.method_x = function(param) {
  6.     console.log(this.foo + ' ' + param);
  7. }
  8.  
  9. function OBJ_Y() {
  10. }
  11.  
  12. OBJ_Y.prototype.method_y = function(cb) {
  13.     cb('whatever_param');
  14. }
  15.  
  16. var ox = new OBJ_X;
  17. var oy = new OBJ_Y;
  18.  
  19. oy.method_y(ox.method_x.bind(ox));
(b) using the call method:
Expand|Select|Wrap|Line Numbers
  1. function OBJ_X() {
  2.     this.foo = 'bar';
  3. }
  4.  
  5. OBJ_X.prototype.method_x = function(param) {
  6.     console.log(this.foo + ' ' + param);
  7. }
  8.  
  9. function OBJ_Y() {
  10. }
  11.  
  12. OBJ_Y.prototype.method_y = function(ctx, cb) {
  13.     cb.call(ctx, 'whatever_param');
  14. }
  15.  
  16. var ox = new OBJ_X;
  17. var oy = new OBJ_Y;
  18.  
  19. oy.method_y(ox, ox.method_x);
(c) using the apply method:

this just replaces line 13 in the above example (b) with:
Expand|Select|Wrap|Line Numbers
  1. cb.apply(ctx, ['whatever_param']);

i hope the problem got a bit more clear with the example and how you can use the methods that are mentioned in the article.
Mar 21 '18 #2
Hi Gits,

Thank you for your addition.

The examples are really helpful...

Moving forward, I'll keep on adding the coding examples to make the concepts more clear.
Mar 22 '18 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bruce W...1 | last post by:
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP....
2
by: Simon Wigzell | last post by:
What is the call? Better yet is there a link to a page that describes all the possible javascript calls to the various browser menu items and explains compatibility across different browsers?...
1
by: Guadala Harry | last post by:
Just wondering what it would take to call a static method in a static class of an ASP.NET 1.1 Web app from a different application. Specifically, I have a little "utility application" (C# Windows...
1
by: Bryan | last post by:
Hello, I am taking values from a datatable and formatting them for use in a JavaScript Function Call. The end result is a mouse over tool tip. Here is what I am doing. I have tried to use...
3
by: Mae Lim | last post by:
Hi All, I've a problem here, I want to trigger a javascript function after binding the ListBox control. Basically the javascript will remove the duplicates records after recordset is bind in the...
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
2
by: Anees | last post by:
Hi, i need to invoke a javascript function while clicking a button that is in a flash movie. i hav used geturl() to redirect the page to some other page. but is it possible to call a javascript...
2
by: Guoqi Zheng | last post by:
Dear Sir, I have an existing application done in ASP.NET(VB.NET), I need to change one page of it. Because I am not able to recompile the code and upload the new dll, I am thinking to use one...
1
by: hofsoc20 | last post by:
Hi, I am trying to call java methods from a python script. Jython is not an option. I have looked at using Jpype. Below is my code thus far: Jpype.py from jpype import * ...
7
gskoli
by: gskoli | last post by:
Dear all, Let me tell you the scenario , i have called javascript function on radio button selection , Ex. Suppose There are 3 Radio Button . Let us consider i have clicked on one radio...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.