Javascript call() & apply() vs bind()?

后端 未结 22 2299
醉话见心
醉话见心 2020-11-22 02:42

I already know that apply and call are similar functions which setthis (context of a function).

The difference is with the way

22条回答
  •  执笔经年
    2020-11-22 03:28

        function sayHello() {
                //alert(this.message);
                return this.message;
        }
        var obj = {
                message: "Hello"
        };
    
        function x(country) {
                var z = sayHello.bind(obj);
                setTimeout(y = function(w) {
    //'this' reference not lost
                        return z() + ' ' + country + ' ' + w;
                }, 1000);
                return y;
        }
        var t = x('India')('World');
        document.getElementById("demo").innerHTML = t;
    

提交回复
热议问题