0

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/

Community
  • 1
  • 1
Jords
  • 3
  • 2
  • 1
    It's not used there because it's not method chaining. Method chaining is when you call a method, then call another method on the result of that method, which may or may not be the original instance itself. In this case, the user isn't calling Math.min at first, they're just sort-of extracting it, and then applying it to an array of values. (There's only one call, not multiple chained calls.) – Jeremy Mar 22 '16 at 17:25

2 Answers2

2

.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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
-1

Appending () to a property name takes the value of that property and tries to call it as a function.

Functions are objects, so can have properties of their own.

apply is a property of the min (and every other) function (i.e. it is not a property of the return value of calling min).

This has nothing to do with method chaining, which is where the return value of a method is the object upon which the method was called.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335