Function.prototype.apply.call

Source: Internet
Author: User

Let's start with a simple topic that we saw on git a few days ago:

Defines the log method, which can proxy console.log methods.
Log (1 2 3)

Usually, your answer will be this:

function log () {    var args = Array.prototype.slice.call (arguments);           Console.log.apply (console, args); };

Until one day, you see a very tall on the answer:

function log () {  Function.prototype.apply.call (console.log,console, arguments);};


Function.prototype.apply.call .... What the hell is this???

Well, then you must know
Function.prototype.apply, and we'll cache it:
var core_apply=function.prototype.apply;
Core_apply.call (console.log,console, arguments) we can break it down

1, call core_apply method, Console.log is core_apply This method of this point, console,arguments is to pass to core_apply this method parameter.

In other words, it is actually equivalent to

Console.log.core_aply (console,arguments);

=

Call the Console.log () method, this points to the console, and the passed-in parameter is arguments.



Function.prototype.apply.call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.