1

I'm relatively new to JavaScript and I was doing some open-source learning reading the jQuery source code. I noticed some, not all, functions are defined 'circular', for instance

var isWindow = function isWindow( obj ) {
        return obj != null && obj === obj.window;
    };

My thoughts:

  1. I know what the code does. It checks if an object is a window object, since it has the special property that it's field .window points to itself: window === window.window === window.window.window...

  2. Does it have anything to do with hoisting, perhaps? if so, why would it be good to have isWindow function undefined at start? And once it reaches declaration, why not use anonymous function?

Thank you

Mc-Ac
  • 161
  • 1
  • 8
  • 1
    *And once it reaches declaration, why not use anonymous function?* IIRC, always using named functions *used* to help with debugging, but that's not the case anymore with recent Javascript engines – CertainPerformance Nov 04 '19 at 07:15
  • 1
    They are not defined twice, nor does it relate to hoisting. `var isWindow` defines a *variable* called "isWindow". `= function isWindow()` then assigns a named function expression to it. – VLAZ Nov 04 '19 at 07:15
  • 1
    Does this answer your question? [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – VLAZ Nov 04 '19 at 07:17
  • Perhaps the purpose is to *avoid* hosting (or maybe it's just a linting rule they use) – CertainPerformance Nov 04 '19 at 07:17
  • 2
    @emrhzc Naming a function helps with debugging, since the stacktrace will use the names instead of lots of (anonymous). – Sheepy Nov 04 '19 at 07:19
  • @CertainPerformance yeah, I can't say for sure why they chose variables vs function definitions. It's certainly possible it's to avoid hoisting but it's also just a linting rule, too (as in, not specifically chosen to avoid hoisting). One other option is to aid in minification, since `function someName` doesn't minify `someName` but `var someName` would, so if you use it multiple times, you get less of a footprint. I know the jQuery team is really rigorous in keeping the size small. – VLAZ Nov 04 '19 at 07:19
  • @VLAZ No, not really. I know the difference between function expression and function declaration, I just can't wrap my head around why someone would define it like that (both methods at the same time). Like, why not just use one methodology. – Mc-Ac Nov 04 '19 at 07:20
  • @Mc-Ac they *are* using one. One of several possible. – VLAZ Nov 04 '19 at 07:21
  • Two reasons: 1. The function name will be shown in the call stack instead of _anonymous_. 2. To call the function recursively by name. – Tushar Nov 04 '19 at 07:21
  • I am unaware of any advantage in using var, if it will not be changed, so I assume it's an artifact of how [jQuery code](https://github.com/jquery/jquery/tree/3.4.1/src) is organized (core modules are included directly, var modules are included as var when required). If you're just learning JS, I recommend sticking with simple function (no var), and get into the habit of using `let` instead of `var`. The later is scoped and will prevent stupid mistakes such as redeclaration. – Sheepy Nov 04 '19 at 08:13

0 Answers0