Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Help in understanding apply method

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #150898
    Kuzyo
    Participant

    Hi everyone. Have a simple function, everything works fine, but I can’t understand how in apply method works this. Here code:

    First:

    var calculate = function() {
        var fn = Array.prototype.pop.call( arguments );
        return fn.apply( this, arguments );
    }
    
    var sum = function( x, y ) {
        return ( x + y );
    }
    
    var sumResult = calculate( 2, 2, sum );
    
    console.log( sumResult );
    

    The same code works if I insted this, write null :

    var calculate = function() {
        var fn = Array.prototype.pop.call( arguments );
        return fn.apply( null, arguments );
    }
    

    I know that first argument in apply is object in context, but if I write null what context there is?

    Can somebody explain? Thanks.

    #151010
    Kuzyo
    Participant

    Thanks @CrocoDillon for profound explanation. Everything became clear. Didn’t hear you long time…

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.