Function.prototype.call (JS) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 19

Function.prototype.call (JS)

What is the difference between: (function (){ //Do something })(); And: (function () { //Do something }).call (this); I have no idea why google employees did this and not that although Both's 'this' argument is window itself

28th Jun 2019, 2:05 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
5 Answers
+ 11
‎‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‎‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‎‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏Space{#learn_to_Smile}☺☺ Hmm... I'm not sure why the 2nd IIFE is invoking the .call(this). Nothing in that 2nd IIFE is making use of the binding for the `this` keyword. 🤔 Even if it was being used, it's obvious the context is the window object, so it's simply unnecessary. Did you use the pretty format feature on minified, obfuscated code? It could be related to a preprocessor rule used in the packaging of this code and reversing this left a lot of the artifacts like what we're seeing here. 🤷‍♂️ Another thought is the JS was transpiled from another language and this was the generic, safe output for that code. The inconsistency between the 1st and 2nd IIFEs could be from different codes transpiled from different modules managed by different teams. 🤷‍♂️ cc: Gordon - I'm mentioning you here since you pinged me about this in the other post. 😉 https://www.sololearn.com/post/116380/?ref=app
28th Jun 2019, 6:24 AM
David Carroll
David Carroll - avatar
+ 12
Gordon 😎Cybrarian😎 This is what I was talking about. I was observing how Google employees coded Google search page so much secured. I found many weird codes as like following https://www.sololearn.com/post/116380/?ref=app
28th Jun 2019, 3:45 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 10
David Carroll Thanks. It seems I got some idea on that.
28th Jun 2019, 6:27 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 5
no, this is not always window, see demo: https://code.sololearn.com/WYLzKgJVzj8E/?ref=app this always refers to the instance of object in which the IIFE is defined. It is therefore safer to use call(this).
28th Jun 2019, 2:47 AM
Gordon
Gordon - avatar
+ 2
Not sure if it's this but, the second one looks more readable than the first one.
28th Jun 2019, 2:28 AM
Alexander Santos
Alexander Santos - avatar