zod zod - 11 months ago 26
Javascript Question

Why write ".call(this)" at the end of an javascript anonymous function?

I have seen JavaScript written like this (it was at a demonstration, and I don’t have the actual code at hand, but it was implied this was normal):

(function() {

var a = 1;

this.sayA = function() {



I suppose it is written an an anonymous function so that the variable
is not globally available.

What could the point of the
be? Since this function was not nested,
was just the window. How does it differ from just writing
at the end?


Try this:

function Foo() {

  (function () {
    // > Foo

  (function () {
    // > undefined in strict mode, or Window in non strict mode

var bar = new Foo;

So, if for whatever reason you use this, it's a way to make the IIFE act as if it were a member function of Foo, specifically when creating instances of a user-defined object type.