Calling Objects Method in JavaScript


In this article I am going to explain about calling objects Methods in JavaScript.

First we will decide what is the difference between method and function:

                It seems that methods and function are same and technically they are in JavaScript, because all functions belong to some object but those methods which are called without  an object (Dot) prefix will be considered as functions because they are part of global namespace. Global functions does not require dot prefix and are owned by the window object. We can see that isNaN(), alert() and eval () are Global functions, they can be directly called, but we can also access it with window object for eg. window.alert() will also work same as alert() works.

Function Call () and Apply ():-

The above function named as Call () and Apply () allows an object to use other methods object.  This save us from having duplicates methods in several objects.

Syntax for using Call () and Apply (): (thisArg[, arg1[, arg2[, arg3[,…]]]])

Function.apply(thisArg[, argArray])

There are scenarios when we will be using call and apply functions; call function is best suited when we know the argument in advance. On the other hand apply function is used for methods when we do not know the argument and argument list can vary.

Leave Comment