Global object state

Let's try another script. The following code defines the fun local variable, which is assigned an anonymous function that returns a number:

var fun = function() {
    return 42;
}

You can then call fun() like a regular function, as follows:

Now, let's look at what happens if we delete the definition of fun from the script, but still keep the invocation:

We still get the same result even though we didn't define what fun means! This is because any variables at the top scope become properties of the global object. The state of ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.