Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • difference between call and apply in javascript?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 110
    Comment on it

    call() and apply() are two predefined javascript methods. We use both of them to invoke a function & these method must have its own object as a first parameter.

    apply allow us to invoke the function with passing the arguments in the form of array.

    Example:

    function myFunction1(a, b) {
                return a + b;
            }
            var myobj1 = new Object();
            var result1 = myFunction1.call(myobj1, 5, 6);
            console.log('Result with call function: ' + result1);
    

    call always requires that all parameters will be listed explicitly not implicitly and one by one.

    Example:

    function myFunction2(a, b) {
                return a + b;
            }
            myArray = [5, 6];
            var myobj2 = new Object();
             var result2 = myFunction2.apply(myobj2, myArray);   
             console.log('Result with Apply function: ' + result2);
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: