Block-Level Scoping in ES 2015

Introduction:
Scoping in JavaScript works little different compared to other languages. Without knowing this, many new and experienced developers have fallen prey to Scoping System in JavaScript. This is because of our coding habits in C#, Java or C++.
The Problem:
We usually create variables in for loop or if condition in a function and think that it will not be accessible outside that loop or if block.
Let us see this example, we are creating a variable ‘name’ in if-condition and try to print it in console outside the if-block. One may think that it will output undefined. But to our surprise, the value that is set in if condition will be printed.

function testMethod() {
     if(true) {
           var name = 'Pranav';
           console.log(name); // outputs 'Pranav'
     }
     console.log(name);  // will output 'Pranav'
}

The reason for this behaviour is that unlike other languages JavaScript follows Function-level variable ScopingJavaScript hoists variables to the top of the function no matter where they were declared in the function enforcing a function-level variable scoping.

This behaviour is evil at times and cause potential problems in large scale apps.
To solve this problem, ECMA Script 2015 ( ES 2015) introduces block-level scoping using two new keywords: let and const.
The Solution: Block-level Scoping using let and const
Both let and const create variables that are block-scoped – they only exist within the innermost block that surrounds them i.e., if-blocks, loop-blocks, anything with curly braces and naked curly braces too.
let:
Let us see an example to make it more clear.

function testMethod() {
     let name = 'Ainavolu';
     if(true) {
           let name = 'Pranav';
           console.log(name); // outputs 'Pranav'
     }
     console.log(name);  // outputs 'Ainavolu'
}

Here the scope of variable name in if-condition is limited to that block. It shadows the function-level variable in that context.
Another good thing with let and const is that they have temporal dead zone (TDZ). When entering its scope, it can’t be accessed (get or set) until execution reaches the declaration.

function testMethod() {
     name = 'Ainavolu'; // uninitialized binding - reference error
     console.log(name); // reference error
     let name; // TDZ ends; name is initialized with undefined
     console.log(name);  // outputs 'undefined'
     name = 'Pranav';
     console.log(name); // outputs 'Pranav'
}

const: 
const creates immutable variables i.e., variables whose value cannot be changed.

     const FILE_NAME = 'MyTestLogFile.log';
     FILE_NAME = 'AnotherLogFile.log'; // throws TypeError

Variables created with let are mutable. The value of those variables can be changed any time after declaration unlike const.
Here is an interesting case of const covered by Axel Rauschmayer.

    const obj = {};
    obj.name = 'Pranav';
    console.log(obj.name); // Pranav
    obj = {}; // TypeError
const does not affect whether the value of a constant itself is mutable or not: If a constant refers to an object, it will always refer to that object, but the object itself can still be changed (if it is mutable).
To make an object mutable, we need to use its freeze property.
const obj = Object.freeze();
obj.name = 'Pranav'; // TypeError

Recommended reading: Variables and scoping in ECMAScript 6 by Axel Rauschmayer
 

Facebook Messenger Channel added to Bot Connector

Microsoft has recently added Facebook messaging channel on Bot Connector. For connecting your Bot to Facebook via Page. That means Users can talk to your Bot using the Messaging Section of the Page. So, you need to create a Page or get the Page ID of existing Facebook Page. Usually it will be under Page Info of your Page Settings.
I am experimenting the API. Will share the sample App once I am done with it. This information is not shared in Bot Framework Blog nor shared anywhere. It is informed via emails for those who created Bots using Bot Framework earlier. So, thought to share it here.
Facebook Channel of Bot Connector

Automated Testing & Auto Scaling Apps with Microsoft & Open Source Technologies

Session presented at DevOps on Azure – Community Roadshow at Hyderabad, organized by MVPs and MUGH.

Here is the link for Presentation Notes for Demos that are shown in the session which covers step by step procedure on how to create IntelliTest, Load Test and Auto Scale your app. Continue reading “Automated Testing & Auto Scaling Apps with Microsoft & Open Source Technologies”

DevOps Community Roadshow 2016

DevOps Community RoadShow 2016
Join us at DevOps Community Roadshow at Westin, Hitec City, Hyderabad on 7th May, 2016. I am going to speak on Automated Testing & Auto Scaling Your apps with Microsoft & Open Source Technologies and discuss the benefits of running load, integration and unit tests automatically attached to Continuous Integration or other means.
Register at https://www.microsoftevents.com/profile/form/index.cfm?PKformID=0x2049354b9c
 

Building a Conversational Bot using Bot Framework and Microsoft Azure

Have presented a session on “Building a Conversational Bot using Bot Framework and Microsoft Azure” at Global Azure Bootcamp hosted by Microsoft User Group Hyderabad (MUGH) at Microsoft IDC, Hyderabad.
This session covers framework overview and step by step process for developing a conversational bot by adding features one after the other making the bot intelligent. Continue reading “Building a Conversational Bot using Bot Framework and Microsoft Azure”

Kick starting with Bot Development

Introduction
Bots don’t need any introduction. They aren’t new and definitely are not unique to Microsoft. Bots are conversational agents. They’re meant to help users achieve and/or complete a particular task. Bots can help users interact with a particular service or application without having to download a separate app or go to a specific Web site. Personal digital assistants like Cortana, Siri and Alexa could be construed as bots. Continue reading “Kick starting with Bot Development”

Why should we add “shim” for AngularJS when using Require.js?

In computer programming, a shim is a small library that transparently intercepts API calls and changes the arguments passed, handles the operation itself, or redirects the operation elsewhere.Shims typically come about when the behavior of an API changes, thereby causing compatibility issues for older applications that still rely on the older functionality. In these cases, the older API can still be supported by a thin compatibility layer on top of the newer code. Shims can also be used to run programs on different software platforms than they were developed for.

-Wikipedia

When using require.js, we usually write as follows. Continue reading “Why should we add “shim” for AngularJS when using Require.js?”