A small example of why to "use strict" mode.

Greetings,

Summary:
  • This plugin is intended solely to learn JavaScript, not to really use.
  • Hence it is posted in Learning JavaScript forum on purpose.
Details:
I wrote my first plugin to show why to "use strict" mode:
  • Basically strict mode catches accidental bugs in your program & saves hour of wasting time chasing bugs.
  • In the example given, I am trying to assign to variables named 'bug' & 'dog'.
  • Due to bad coding it creates a global variable 'bug' (instead of local one if I had use the 'var' keyword).
  • However, in the second example due to "strict mode" it catches my bug & does not create a global variable 'dog'.
Output:
Spoiler: Here is the output from the plugin on the console
upload_2017-12-3_11-47-54.png

Downloadable:
You can download the plug in from this link: Bloodstone.js​

Please let me know if anything is unclear.​

Thanks,

Joy Diamond.

Spoiler: Also here is the full code for the Bloodstone.js plugin
Code:
//
//  Copyright (c) 2017 Joy Diamond.  Licensed under the MIT License.
//  BLOODSTONE: Bugs Lack Object Oriented Development.  Strict Testing Obligatory, No Errors
//
//  This plugin demonstrates why to use strict mode:
//      For more details please read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
//


//
//  Since this plugin is only for demo purposes in the Developer Tools:
//      Bring up Developer tools by default in test mode.
//
if (Utils.isOptionValid('test') && Utils.isNwjs()) {
   if (Utils.RPGMAKER_VERSION == '1.5.1') {                //  Is this RPG Maker MV 1.5.1?
       //  Show developer tools (nw.js older version)
       require('nw.gui').Window.get().showDevTools()
   } else {
       //  Show developer tools (nw.js 0.25.4 version)
       nw.Window.get().showDevTools(false)
   }
}


//  BAD: Bad coding style
(function BAD(){
   //  "use strict"                                        //  Bad coding style: missing strict mode!!!

   bug = 666                                               //  Will *NOT* throw an error
})();


//  GOOD: Good coding style
(function GOOD(){
   "use strict"                                            //  Strict mode helps catch JavaScript errors, very useful

   window.cat = 777                                        //  Here we create a global on purpose using `window.cat`

   dog = 888                                               //  Will throw an error, and not create `dog` by mistake
})();


//--------------------------------------------------------+
//  This code is formatted for clarity.                   |
//  Hence this code does not use unnecessary semicolons.  |
//  Reasoning: https://mislav.net/2010/05/semicolons/     |
//--------------------------------------------------------+


//  The full MIT License is available here: https://github.com/Rhodolite/Opal/blob/master/LICENSE
/*: @plugindesc Bugs Lack Object Oriented Development.  Strict Testing Obligatory, No Errors */

P.S.: For more details on why to "use strict" mode please see Strict Mode by Mozilla Development Network

Original thread: https://forums.rpgmakerweb.com/threads/a-small-example-of-why-to-use-strict-mode.88036/