Global To-String
JavaScript Corners - Part 9

Global To-String
JavaScript Corners - Part 9

(This is Part 9 in my series on JavaScript corner cases). Here’s another one. In JavaScript, global variables are properties of the global object. By default, the global object is like any other, and inherits from the Object.prototype  object.  Object.prototype comes with a number of its own properties, such as the  toString method. So, that means that toString is also …

Read More Read More

JavaScript Corners – Part 9
Node.js With-Statement Bug

JavaScript Corners – Part 9
Node.js With-Statement Bug

What does the following evil code print?

If you’re not sure, don’t worry — neither are current JavaScript engines. Firefox prints “after”, while Edge, IE, and Node.js print “before” (node v7.9.0). I believe that Firefox is correct in this case. The tricky statement is obviously the following one, which sets a property on an …

Read More Read More

C++ vs JS
Binary Type Coupling

C++ vs JS
Binary Type Coupling

I was chatting to some of my colleagues at work recently about the potential benefits of a JavaScript compiler vs a C compiler, when targeting an embedded MCU. A concern that came up multiple times regarding different features, is …but you can already do that in C, so how is JS better? This is a …

Read More Read More

JavaScript Corners – Part 8
References (Continued)

JavaScript Corners – Part 8
References (Continued)

Given an object o  with a member function f  that prints out what the this value is:

We know what the following prints:

And we know what the following prints1:

I always thought that the difference came down to the fact that o.f()  is actually invoking a different operator — something like a “member call …

Read More Read More

JavaScript Corners – Part 7
Calls and With Statements

JavaScript Corners – Part 7
Calls and With Statements

Here’s a quick one. What does the following print? (Assuming not in strict mode)

In non-strict mode, the naked function call foo() gets a  this value that is the global object. So the first case prints “Global”. In the second case, we’re invoking foo as a member of  bar, and so the this value is bar (it prints “Bar”). The …

Read More Read More

JavaScript Corners – Part 6

JavaScript Corners – Part 6

In what order does the following evaluate?

TL;DR Answer

Step 1: Variable Access First off, what does this code even mean? If you’re not intimate with JavaScript, this might seem like a very confusing line of code. In fact, even if you’re familiar with JavaScript, this can be confusing. So let’s break it …

Read More Read More

JavaScript Corners – Part 5

JavaScript Corners – Part 5

Here’s a quick one:

This was quite unexpected to me. It’s the only time I’ve ever seen where the left-hand side of an assignment can affect the right-hand side. This only happens once. Once the anonymous function has a name, it can’t be re-named:

Interestingly, this doesn’t seem to work with destructuring:

Read More Read More

JavaScript Corners – Part 4

JavaScript Corners – Part 4

Here’s another quick one. It relates to the scoping rules with parameter expressions.

Function foo has three parameters: x, f, and y. The key thing here is that the default value for f is a function that encloses the local lexical scope surrounding the function, and we’ve designed the function in such a way that when we call …

Read More Read More

JavaScript Corners – Part 3

JavaScript Corners – Part 3

I was busy reading ES spec today, and the following question popped into my head: at what point are default parameters evaluated? To answer this question, I wrote the following script:

If default parameters are evaluated when the function is declared, then we would expect ‘a’ to appear once in the terminal output. On the …

Read More Read More