Skip to main content

call, apply, bind method in JavaScript

 The role of call, apply, and bind is to change the context of function execution, and when they are used, they will change the this pointing to when the function is running. call, apply, and bind are all methods under Function.prototype, which are used to change the runtime context of the function. The final return value is the return value of the method you called. If the method has no return value, it returns undefined.

call() :receives a list of parameters

        function fun(a,b){
            console.log(this);
            return a+b;
        }
        let obj={id:100,name:"aaa"};
        // let result=fun.call(obj,100,200);
        let arr=[20,50];
        // let result=fun.call(obj,[...arr]);//20,50undefined
        let result=fun.call(obj,...arr);
        // let result=fun.call(obj,[10,20]);//10,20undefined
        console.log(result);

apply(): receives an array of parameters

    function fun(a,b){
            console.log(this);
            return a+b;
        }
        let obj={id:100,name:"aaa"};
// let result=fun.apply(obj,100,200);//Uncaught TypeError: CreateListFromArrayLike...
        let arr=[20,50];
        // let result=fun.apply(obj,arr);//70
        // let result=fun.apply(obj,[...arr]);//70
// let result=fun.apply(obj,...arr);//Uncaught TypeError: CreateListFromArrayLike...
        let result=fun.apply(obj,[10,20]);//30
        console.log(result);

bind(): The function will not be executed immediately, but a new function after modifying this will be obtained, if parameters are passed when calling bind. The parameters will also be bound, and the parameters cannot be passed when called later. If no parameters are passed when bind is bound, the parameters are not bound. You can pass other parameters later

    function fun(a,b){
            console.log(this);
            return a+b;
        }
        let obj={id:100,name:"aaa"};
        // let result=fun.bind(obj,100,200);//300
        let arr=[20,50];
        // let result=fun.bind(obj,[...arr]);//20,50undefined
        // let result=fun.bind(obj,...arr);//70
        // let result=fun.bind(obj,arr);//20,50undefined
        // let result=fun.bind(obj,[10,20]);//10,20undefined

       /*  let result=fun.bind(obj)
       let c= result(...arr);*///1000
       console.log(c);

💬Summary: If you don't need to care about how many parameters are passed into the function, choose apply();If you determine how many parameters the function can receive, and you want to express the correspondence between formal parameters and actual parameters at a glance, use call(); if the function does not want to execute the function immediately and get the return result of the function, use bind();

Comments

Popular posts from this blog

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.