New Features of JavaScript in Visual Studio 2012

Introduction

In today's article you will learn about a new feature for JavaScript in Visual Studio 2012 i.e. Strict Mode.

The Strict Mode property provides a greater amount of error checking of the code. If you use Strict Mode then you cannot use implicitly declared variables, can't assign a value to a read-only property and can't add a property to an object that is not extensible.

Adding Strict Mode to the code is very simple; you just add the "Strict Mode" directive in the beginning of the code. The physical location of Strict directive determines the scope of Strict Mode. There are four ways to declare Strict Mode:

  1. At the beginning of a .js file: All the code in the program will be in Strict Mode.
  2. At the beginning of <Script>: All <Script> elements will be in Strict Mode.
  3. Declared outside the Function: All the code in the Program will be in Strict Mode.
  4. Declared in a Function: All the code in the function will be in Strict Mode.

Step 1

If you want all the code to be in Strict Mode then you must write the following code:

"use strict";

function variablecheck()

{

    var testvar = 5;

    return testvar;

}

testvar = 4;

In this the variable declaration is outside the function and will generate an error since all the code is in Strict Mode.

Step 2

If you want Strict Mode to only apply to your function then you must write it as follows.

function variablecheck()

{

    "use strict";

    testvar = 5;

    return testvar;

}

testvar = 4;

In this the variable declaration is inside the function and will produce an error but the variable declaration is outside the function therefore it will not produce an error since it's not in Strict Mode.

I am giving you one more example; see the followiing:

$(function ()

{

    "use strict";

    function test()

    {

        alert("I am in strict mode");

    }

    function test2()

    {

        alert("Me too!");

    }

});

In the code above Strict Mode is working on both functions.

Step 3

The following are the elements for which restrictions will be applied by the Strict Mode:

Element Restriction Example
Variable Using variable without declaring it testvar=4;
Read-Only Property Writing to a read-only property

var testObj = Object.defineProperties({},

    {

        prop1:

            {

                value: 10,

                writable: false

            },

        prop2:

            {

                get: function ()

                {

        }

    }

});

testObj.prop1 = 20;

testObj.prop2 = 30;

delete Deleting a function, variable or an argument.

Deleting a property whose configurable attribute is set to false

var testvar = 15;

function Func() {};

delete testvar;

delete Func;

 

Object.defineProperty(testObj, "test",

{

    value: 10,

    configurable: false

});

delete testObj.testvar;

Non-Extensible Property Adding a property to an object whose extensible property is set to false.

Object.defineProperty(testObj, "testvar",

    {

    value: 10,

    extensible: false

    });

delete testObj.testvar;

Duplicate Parameter Name Using a parameter name more then once in a function.

function Func(param1, param1)

{

    return 1;

};

Duplicating a Property Defining a property more then once in object literal.

var testObj =

    {

    prop1: 10,

    prop2: 15,

    prop1: 20

    };
Octals Assigning an octal to a numeric literal, or attempting to use an escape on an octal value.

var testoctal = 010;

var testescape = \010;

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now