Search code examples
javascriptchainingparentheses

Why aren't parenthesis used in this instance of chaining built-in JavaScript methods


I refer to this answer: https://stackoverflow.com/a/8934895/4275690

    Array.min = function( array ){
    return Math.min.apply( Math, array );
};

Howcome Math's min method doesn't require parenthesis before .apply() is chained onto it? My understanding of that syntax is that .min without parenthesis is a property.

My confusion arises because the majority of my experience with chaining comes from using jQuery with code similar to the following:

    jQuery("div").hide("slow", function(){
      jQuery(this)
      .addClass("done")
      .find("span")
      .addClass("done")
      .end()
      .show("slow", function(){
        jQuery(this).removeClass("done");
      });
    });

source: http://ejohn.org/blog/ultra-chaining-with-jquery/


Solution

  • .apply isn't a chained call, it's a property of every Function, it being available because every function inherits from Function.prototype.

    That is, given:

    var m = Math.min;
    

    Then m.apply is just accessing the .apply property of that function.