The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

infos@wais-fr.com (29) [Avatar] Offline
#1
The explanation about the second parameter named undefined is really confusing :

<<There's a benefit to aliasing undefined like this. If the original object has >
been modified by code elsewhere in the current execution environment, it won't
affect your code, because you're using a local alias that has the original, untouched
value. You'll find this is a common technique used by JavaScript library authors.>>

The last phrase in this excerpt let me think this way of coding is for geek only. It lets me think that if you are not a Javascript library author you are not at the level of understanding the details of this undefined parameter.

BUT your book is about how to write a Javascript library to be used as third-party library !

For me this explanation of the undefined parameter is the second failure in this book (sorry to be so hard !)

I am not a Javascript geek as you might be.
Nonetheless, your explanation of the undefined parameter is like a politician story that the reader must trust.

I suggest you replace your explanation with sometinh similar to the following:

Javascript has built-in types (like any other programming language). These built-in types are : undefined, null, boolean, number, and string.
When an object or a variable has not been assigned, its value is set to undefined.
As an example, execute this small piece of Javascript :
<script type="text/javascript">
var foo;
alert("foo=" + foo);
</script>
The result should be foo=undefined.

undefined if often used to check, at runtime, if an object has an existing property either to dynamically add this property or to skip the current object.
As an example, execute this small piece of Javascript:
<script type="text/javascript">
var Stork = {}; // creates an empty object;
alert(typeof Stork); // must display object

alert(Stork.foo); // must display undefined

if (Stork.foo === undefined) {
// the foo property does not exist
// so create it
Stork.foo = "a new foo property";
}

alert(Stork.foo); // must display "a new foo property"
</script>
Imagine now that you need to provide the Stork object as an entry point for a custom Javascript library. To do this, you should refactor the last example like this :

<script type="text/javascript">
var Stork = (function (window) {
var Stork = {};

if (Stork.foo === undefined) {
Stork.foo = "a new foo property";
}

return Stork;
})(window);

alert(Stork.foo); // must display "a new foo property"
</script>

The above example should work every time in the same way. As you might expect the alert(Stork.foo) should always return "a new foo property".
Now let's dive into the dark side of Javascript. Just before the above example add the following script :
<script type="text/javascript">
var undefined = true;
</script>
Now execute both scripts in sequence. The alert shows now "undefined" instead of the standard message.
Your Javascript library has been broken with just a single line of code !
The problem is that the built-in type called undefined can be redefined to anything. There is no internal mecanism to protect this built-in type !
So now that you know that anyone can break your Javascript library with just a single line of code, the question is : How to protect myself against poor Javascript programmers?
You will have to refactor the above sample like this :
<script type="text/javascript">
var Stork = (function (window, undefined) {
var Stork = {};

if (Stork.foo === undefined) {
Stork.foo = "a new foo property";
}

return Stork;
})(window);

alert(Stork.foo); // must display "a new foo property"
</script>
The trick is to add in the function declaration an additional parameter (called undefined) that you will NEVER USE.
Now execute both script in sequence and you will see that your library works as expected !


Best regards
Henri d'Orgeval
benvinegar (68) [Avatar] Offline
#2
Re: 2.3 The initial script file
Hi Henri,

I like your explanation a lot - it's better on every level. The problem is that this is a relatively minor note (on undefined), and we don't want to spend a lot of time on it. I think we could add a few more sentences to make it clearer, however.

Thanks,

- Ben