{"id":29856,"date":"2015-11-10T11:07:40","date_gmt":"2015-11-10T05:37:40","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=29856"},"modified":"2015-11-10T11:07:40","modified_gmt":"2015-11-10T05:37:40","slug":"call-vs-apply-vs-bind","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/call-vs-apply-vs-bind\/","title":{"rendered":".call() vs .apply() vs .bind()"},"content":{"rendered":"<p><strong>To start with lets suppose we have a function getCount as defined below-<\/strong><br \/>\n[code lang=&#8221;html&#8221;]function getCount(a,b,c){<br \/>\n  return this.count + a + b + c;<br \/>\n}[\/code]<\/p>\n<p>To provide different scope at the time of &#8220;getCount&#8221; function execution. We generally use these functions<\/p>\n<p><strong>1- .call():<\/strong><\/p>\n<p>[code lang=&#8221;html&#8221;]<br \/>\nvar obj1 = { count: 10 };<br \/>\ngetCount.call(obj1,4,5,6);<br \/>\n\/\/ functionName.call(&#8216;this&#8217; or the scope you want this function<br \/>\n\/\/to refer, argument1, argument2, argument3)[\/code] <\/p>\n<p><strong>Output: 25<\/strong><\/p>\n<p><strong>2- .apply():<\/strong><br \/>\n[code lang=&#8221;html&#8221;]getCount.apply(obj1, [4,5,6]);<br \/>\n\/\/ functionName.call(&#8216;this&#8217; or the scope you want this function to refer, array of arguments [argument1, argument2, argument3])<br \/>\n[\/code]<br \/>\n<strong>Output: 25<\/strong><\/p>\n<p><strong>3- .bind():<\/strong><br \/>\nThis is slight different from above both the functions.<br \/>\n[code lang=&#8221;html&#8221;]<br \/>\nvar bound = getCount.bind(obj1)<br \/>\n\/\/functionName.bind(&#8216;this&#8217; or the scope you want this function to refer)<br \/>\nbound(4,5,6);<br \/>\n[\/code]<br \/>\nOutput: 25<br \/>\nNote: To explore more about bind() you can check bound function by using console.dir(bound);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To start with lets suppose we have a function getCount as defined below- [code lang=&#8221;html&#8221;]function getCount(a,b,c){ return this.count + a + b + c; }[\/code] To provide different scope at the time of &#8220;getCount&#8221; function execution. We generally use these functions 1- .call(): [code lang=&#8221;html&#8221;] var obj1 = { count: 10 }; getCount.call(obj1,4,5,6); \/\/ functionName.call(&#8216;this&#8217; [&hellip;]<\/p>\n","protected":false},"author":122,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/29856"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/122"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=29856"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/29856\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=29856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=29856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=29856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}