JS Learning Summary----Call method in depth

Source: Internet
Author: User
Tags function prototype

var obj = {name: "Zhang San"}        function  fn () {            Console.log (this)        }        fn ();         // Obj.fn is not a function         fn.call (obj)        /// First we let the call method on the prototype execute, and when we execute the call method, we make the this in the FN method the first parameter value obj And then execute the FN function.
 //  Self-simulates the built-in call method, writing a Mycall method to delve into the call principle  Function.prototype.myCall = function   ( Context) { /*   Mycall method T His is the name of the function in which I want to manipulate and change the This keyword so that the "this keyword" in this function becomes the context  * / //  1, let the This keyword in FN change to the value of the context->obj
     eval (this . toString (). Replace (' This ' 
// first fn1 to find the call method on the Function.prototype through the prototype chain mechanism, and let call () method execution, and this is the method that I want to operate the FN1, In the call method code execution, first let the "this keyword" in fn1 into fn2, and then let this method execute->1        fn1.call.call (fn2)        /*             first, the call method on the Function.prototype is found through the prototype chain mechanism, and then the call method is found on the function prototype by the prototype FN1 (because the value of call itself is also an So the same can be found Function.prototype), the second time to find the call to the method to execute, the method of this is Fn1.call, and then let this method of this into fn2, and finally Fn1.call execution->2        */

JS Learning Summary----Call method in depth

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.