Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Introduction and Basic Overview of JavaScript "Strict Mode"

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.85k
    Comment on it

    Introduction:

    Strict Mode is one of the new feature introduced in ECMAScript5 edition.

    Strict Mode is a group of the language that remove disfavor features. The Strict mode is not mandatory, it means that if we want our code to run in the strict mode, we can declare the expression as once per function or for the whole program by adding following string:

    "use strict";

    The Strict Mode (use strict) Declaration:

    The main objective of use strict is to specify that the code or query should run in Strict Mode.
    We can use the strict mode by adding use strict; at the starting of a function, a program, or a file. The main scope of a Strict Mode statements depends on the environment or events.

    For Example -

    If use strict; is mention in global environment or context, all the code in the program will execute in strict mode.

      "use  strict";
    
      function  testStrictFunction(){
           var  testVal = 3;
        return  testVal;
      }
      //  This causes a syntax error.
      testVal  = 5;
    

    If use strict; is mention in a function, all the code in the function will execute in strict mode.

      function  testStrictFunction(){
        "use  strict";
    
        //This causes a syntax error.
        var  testVal = 3;
        return  testVal;
      }
    
      testVal  = 5;
    

    The Strict Mode Limitation:

    As we already mentioned that its a new feature of EcmaScript5, so there are few limitation rise in Script Mode implementation. These are given below.

    => Browsers Limitation

    From Browsers point of view, strict mode also have the limitation as per the different browser and its versions.
    Here are the following list given below.

    Browser Name Version
    Internet Explorer From onward version -10
    Moziall Firefox Form version - 4.0
    Chrome From version - 13
    Safari From version - 5.1
    Opera From version - 12

    => Code Restriction or Limitations

    Except browsers limitation, Strict Mode have some code restrictions. The following table lists of code restrictions are given below.

    Code Attributes / Elements Limitations or Restrictions Error Display
    Global Variables

    Strict Mode never understand the Globally define variable.

    This global variable declaration behavior can be problematic if any variable is already defined elsewhere and is expected to have a different value.

    SCRIPT5042:

    Variable undefined in strict mode

    Read-Only Property

    Writing any value to a read only properties.

    Attempts fail while writing any value to read only property

    SCRIPT5045:

    Assignment to read-only properties is not allowed in strict mode

    'with' statement

    In strict Mode, with statement is not allowed.

    Any Value or variable is ignored inside the with statement.

    SCRIPT1037:

    'with' statements are not allowed in strict mode

    'this' method Usage

    The value of 'this' is not recognize in Strict Mode.

    Attempts fail while using 'this' method in program to get the value globally

    Null or Undefined

    Identifier Naming

    Many restricted properties such as variables, functions, and parameters are places in Strict Mode.

    Eval and Arguments strings are also restricted to use as an identifier and can't be assign any value on it.

    Variable name can't be eval or arguments in strict mode
    Additional Identifiers Keywords
    • implements
    • interface
    • let
    • package
    • private
    • protected
    • public
    • static
    • yield

    Can't use following future reserved keywords in identifiers

    Null or Invalid Statements

    Duplicate Parameter name

    Script Mode not allow objects to contain multiple properties with the same name.

    Strict Mode specify that all properties name will be unique.

    SCRIPT1046:

    Multiple definitions of a property not allowed in strict mode.

    Non-extensible Objects and Variables In strict Mode, attempt fail to adding property to non-extensible object and variable. SCRIPT5046:

    Cannot create property for a non-extensible object.

    Delete In strict Mode, attempt fails to delete any variable, or function, or any argument. SCRIPT1045:

    Calling delete on <expression>is not allowed in strict mode

    Few Examples of Strict Mode:

    => Example - 01

    (function() {
        "use strict";
        var a;
        var b;
        function bar() {
            x = 5; //Strict Mode will throw an error for implicit globals
        }
        bar(); //ReferenceError (Strict Mode is enforced)
    })();
    

    => Example - 02

    var container = function(e) {
        'use strict';
        var deleteNonConfigurable = function () {
            var obj = {};
            Object.defineProperty(obj, "name", {
                configurable: false
            });
                delete obj.name; //Will throw TypeError in Strict Mode
        }
        return deleteNonConfigurable;
    }
    wrapper()(); //TypeError (Strict Mode enforced)
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: