Chapters
33 Chapters
|
3:02:08
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.2.0
Subscribe to download the code!Compatible PHP versions: ^7.2.0
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
13.
The window Object & Global Variables
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial uses an older version of Symfony... but since it's a JavaScript tutorial, the concepts are still ? valid!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.2.0",
"symfony/symfony": "3.1.*", // v3.1.10
"twig/twig": "2.10.*", // v2.10.0
"doctrine/orm": "^2.5", // v2.7.1
"doctrine/doctrine-bundle": "^1.6", // 1.10.3
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
"symfony/swiftmailer-bundle": "^2.3", // v2.4.0
"symfony/monolog-bundle": "^2.8", // 2.12.0
"symfony/polyfill-apcu": "^1.0", // v1.2.0
"sensio/distribution-bundle": "^5.0", // v5.0.22
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"friendsofsymfony/user-bundle": "~2.0@dev", // dev-master
"doctrine/doctrine-fixtures-bundle": "~2.3", // v2.4.1
"doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"friendsofsymfony/jsrouting-bundle": "^1.6" // 1.6.0
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.1
"symfony/phpunit-bridge": "^3.0" // v3.1.6
}
}
14 Comments
I think another good way how to hide private functions would be using "Revealing Module Pattern". You don't need to create any Helper object and as a bonus you get rid of binding "this" to the context. Using this approach you just return from within self-executing function only the methods you want to be public. Another advantage is you don't need to bind anything to window object. Instead you assign self-executing function to a variable.
Hey Lukáš Pápay!
This is great! I've seen this pattern... but I don't know why I didn't show it! Honestly, while it has the same effect, I like returning a var from the self-executing function better than modifying window inside, actually :).
Anyways, thanks for sharing! Very good example!
Adding 'use strict' should be done only inside the IIFE. The strict mode directive applies to the current scope and all child scopes. By putting it outside the IIFE, it puts the global context in strict mode and so all contexts as well, which is likely to break third-party libraries not written as strict.
By putting it inside the IIFE, only your own code (which lives inside the IIFE) will be turned into strict mode
Yo Stof!
Actually, we should definitely add a note about the scope of "use strict" - you're absolutely right. But, I don't believe that adding it at the top of the file affects other files (e.g. http://stackoverflow.com/qu... - each file is considered its own "program" and acts independently. I just verified this by keeping the "use strict" at the top of RepLogApp.js, and then setting a variable without "var" inside of index.html.twig. But, as the StackOverflow mentions, if you perform a simple concatenation of your JS files, then you can still have a problem. So I'm going to add a note about this!
Thanks!
Ah, I was not aware that it was limited per file. But I concatenate my JS files all the time anyway, which may be why I missed this distinction.
I wasn't exactly sure myself - you made me do further research ;). We'll have a note added to text & then video in the coming days.
Thanks for the tip!
Now I know why we use window and $ in self executing function as argument, excellent explanation. As a PHP dev, it always makes my head spin when I working with js. My headache is getting better.
Hey Yan,
Haha, we're glad you like it! Yea, JS become really fun and easy if you start understanding it better. ;)
Cheers!
Only one thing I didn't understand. You said that passing jQuery as an argument eliminates the possibility of it be set to null. But when we add the 'window' argument, we could modify it (adding the RepLogApp). Maybe because 'window' argument has the same name as global 'window', the code inside the function actually used the global variable?
Hey boykodev!
Great question - good attention to detail :). When the code inside the function references
window, it's referencing thewindowargument that's passed into the function (at the very top). This, by chance, has the same name as the global variable, but that's not important. Let me give you an example. This code would also work:In this case, we are still ultimately modifying the global, window object - but we just gave it a different name. So.... I guess my point is this: in the original code, by passing
windowas an argument, it means that when we referencewindowin the function, it refers to the argument, not the global variable. Of course, the variable is a reference to the global object. But, there is one subtle difference: you can modify window, but you could not accidentally replace it:I hope that explanation wasn't too confusing :). The self-executing function gives you some isolation, and it's important to understand why they're used. But it's still a bit imperfect. The module system (as you'll see in our Webpack tutorial) is a more perfect system.
Cheers!
Wasn't confusing at all, thanks! What about if you still want to access a global 'window', inside the function with 'window' argument. Is there a way to do this?
Awesome :). Well, you still are referencing the global window argument. In some ways, it's no different than this in PHP:
The only thing that you absolutely will not be able to do if you have the
windowargument is set the global window variable to a completely different value (e.g.window = 'foo'), because this will only change the local variable. And there's no way around this (if you have the window argument), which is really the point of the self-executing function :).Cheers!
Oh, I see...you can modify the object, you just can't set it to something else. It's some sort of a foolproof system :)
Exactly right :). It's similar to the
constkeyword you'll learn about in our ES6 tutorial: a constant can still be modified, just not re-assigned."Houston: no signs of life"
Start the conversation!