Skip to content

Just a few examples of "use strict" vs normal mode

Notifications You must be signed in to change notification settings

besarthoxhaj/use_strict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

"user strict" examples

See official docs and the great article of John Resig.

// SyntaxError: Delete of an unqualified identifier in strict mode.

// case 1;
var a = 'Hello';
delete a;

// case 2
var a = 'Hello';
(function deleteA() {
  var a = 'world';
  console.log(a); // --> world
  var wasDeleted = delete a;
  console.log('wasDeleted',wasDeleted);
  console.log(a); // --> world
}());

/**
 * Rationale
 * Case 1 shows the most basic example of this error, however doesn't
 * tell much about the why we may want to prevent it. Let's look at case 2.
 * Case 2 shows a bit more of why we may not want for this action to work.
 * If was possible to delete `var a = "world"` it means we are able to change
 * dynamically the local scope of a function.
 *
 * Resources:
 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
 * http://blog.caplin.com/2012/01/31/javascript-is-hard-part-3-you-cant-delete-with-delete/
 * http://perfectionkills.com/understanding-delete/#deleting_variables_via_eval
 */

The above means that it is not possible to do something like this (which is a pity):

var a = 'hello';
var b = {
  c:a
};

var aa = b.c;

delete aa;

Declaration and expressions

Consider the following code:

About

Just a few examples of "use strict" vs normal mode

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages