Jords
Jords

Reputation: 3

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/

Upvotes: 0

Views: 292

Answers (2)

Alnitak
Alnitak

Reputation: 339816

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

Upvotes: 2

Quentin
Quentin

Reputation: 943527

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.

Upvotes: -1

Related Questions