Internal methods for Function objects call (), apply (), bind ()

Source: Internet
Author: User

These three functions refer to the function itself, and the first parameter of call () and apply () are the context objects to be executed by the function, and then the arguments are the arguments that the function executes to be passed, and in different places, call is the parameter of the enumeration, and apply is passed as an array. Of course, you can also pass arguments objects. Bind () binds the function to the specified context context execution environment, simply by pointing the function's this object to the parameter context in bind () and returning a function instance.

Simply write an example code and we'll be clear:

<script>functionSayhi (name,greeting) {varName = name| | This. Name, greeting= Greeting| | This. Greeting; Console.log ("Hello" +name+ "," +greeting); }         varName = "Terry", greeting= "How is it?", Me= {Name: "John", Greeting:Hello "}; Sayhi.call ( This, name,greeting); Sayhi.call ( This); Sayhi.apply ( This, [name,greeting]);    Sayhi.apply (me,[me.name,me.greeting]);    Sayhi.call (me,me.name,me.greeting); //bind () returns the function instance, followed by () to execute directlySayhi.bind () ();    Sayhi.bind (Me) (); </script>

Execution Result:

Internal methods for Function objects call (), apply (), bind ()

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.