Sheraz Lodhi's Blog

Monday, November 23, 2015

What is MongoDB?

According to www.mongodb.org
"MongoDB (from "humongous") is an open-source document database, and the leading NoSQL database."
Most of us are more familiar with a different type of databases called "Relational Databases" in which the data is saved in the form of tables and connected with "Relations". Inside the tables the data is divided in to rows and columns and these tables have a defined schema and all records in that table should conform to the schema of that particular table. The schema is defined at the time of creation of the table. For an example:

If we wanted to create a table which could hold Post and its comments then in relation database we will create something like this.

Post:
Name

Comments:
Comment:


But in case of MongoDb the structure will be like the one below.

{

"Post":"this is a post"
"Comments": ["Comment 1",
"Comment 2",.....
]

}

This format is known as BSON , which stands for Binary JSON because it closely resembles JSON format.


Sunday, November 22, 2015

Introducing "class", "constructor" , "static", "extends" & "super" keywords in JavaScript (ECMAScript 6)

Most devs are familiar with most of these keywords in their favorite languages. Now if your favorite language is JavaScript and you were missing these keywords ECMAScript 6 has introduced following new keywords with a bonus keyword "Constructor", i think no other language has this keyword. Below are the new keywords
  • class
  • constructor
  • static
  • extends
  • super

Although they look very similar to other language constructs they are actually different and JavaScript is still supporting only prototype-based inheritance. Let's discuss each of them 

class:  

Class declarations are also a type of functions and the work almost the same way. A simple class can be defined as below:

Another way to create a class in javascript is by using class expressions as shown below

Constructor:

constructor is a method that is used in order to create the object by use of class keyword. You can only have one function using constructor in your class.

Static:

Same as in other languages(c#) this static method is used to create a function that can be run without instantiating the object of a class. They are mostly used to create utility functions.
 

extends:

This can be used to extend any javascript class. When I tried to create use this method to extend a class as shown below, I got an error:


Javascript ES6 class extend error "Uncaught ReferenceError: this is not defined"

Super: 

In order to get rid of this error all I have to do was call a function super(); super method is used to call the functions of objects parent.

ES6 class constructors MUST call super if they are subclasses and want to access "this" keyword.

So, Just uncomment the super() method in above code and everything will work fine as shown below.

Wednesday, June 3, 2015

How to choose a JavaScript framework between AngularJS, Backbone.JS and ember.js

The other day I had a look at the info-graphic below. I really liked the details that are provided just in one image. Let's have a look at this http://www.webdesigndegreecenter.org/choosing-javascript-framework/
Choosing a Javascript Framework
Source: WebDesignDegreeCenter.org

It is a daunting task to figure out which JavaScript framework you should choose for your project. And the above info graphic can provide just enough insight into current overall standings of these frameworks. It compares AngularJS, Backbone.JS and ember.js. As I had figured out after a look of research that AngularJS may be the right choice at this moment for my project , this info-graphic can help you conclude to this in just looking at the image. You can compare all the details side by side for each of the framework. Each framework has its own pros and cons and you should always give a good amount of your time to finalize the framework you want to choose so that it suits well to your project and you don't suffer from any project delays or library related problems in the middle of your project. It is rightly said in the infographic that you are actually selecting your poison, as there are pros and cons for every framework and you should pick the poison for which you have developed immunity. We will call this #JavaScriptPoison 

Wednesday, May 6, 2015

Microsoft Visual Studio Code and Chrome's developer tool??

I was recently using MS Visual Studio code and in order to format the code I used the shortcut control+shift+I and this is what i saw


You can see clearly that it opens Chrome's developer tool (with Microsoft's copyright)? I am unable to understand what relation does this Visual Studio Code has with Chrome's developer tool?

I then tested it by adding an alert and yes that works too :D



Anyone has an idea? do let me know.

Friday, May 1, 2015

JavaScript full stack development (MEAN) using Newly announced Microsoft Visual Studio Code in Ubuntu

Microsoft recently launched Visual Studio Code A free cross-platform code editor for linux, Mac and Windows. I thought to give it a try on Ubuntu.
  • First of all let's download Visual Studio Code from the here  
  •  After downloading extract it.
  • Then copy it to a folder where you put your programs in Ubuntu. I copied it to /Programs/Code
  • After copying the folder now we want to be able to run VSCode by just typing Code in our terminal.
  • So, We will add this to bash.bashrc file by entering the following command in terminal.
  • after the file is opened you will add the path to your VSCode directory to PATH variable at the end of bash.bashrc file as shown in the image below 
  • Now you are ready to open VSCode by entering just one command from your terminal. "Code" Please note that it is with capital "C" as the name of binary file name starts with capital "C"
  • Here you go. You now have Visual Studio Code setup on your Ubuntu and you can start developing.
  • I have started a sample MEAN(Mongodb, Express, AngularJs, Node.js) project and created it folder structure etc for full stack JavaScript development. 



Monday, April 27, 2015

Amazing things about JavaScript

arguments object:
arguments is a default object which is always there for you even when you haven't passed anything to your function. you still have arguments object. and that's the reason why you can send any number of parameters to a JavaScript function with any number of arguments.

For example you can write the code below :

var sum = function(){
var sum=0;
for(i=0;i{
sum += arguments[i]; 
}
console.log(sum);

}

sum();
sum(1,2);
sum('1','2','3');


IFEE:
Immediate function execution expressions are the functions which are executed as soon as they are declared.

e.g
(function(){console.log('run');})();   
Or
(function(){console.log('run');}());   

There are many ways to create objects in Javascript

Literal Object Notation:
Yup this is so similar to Json because json is subset of Literal Object Notation :)

var obj = {
  prop1: value1,
  prop2: value2
};

Object oriented JavaScript:  JavaScript is object oriented but in different ways than regular programming languages. An object in JavaScript can be initialized in the many ways. some are below

Using Constructor functions:
  • Constructor functions have one major difference that they members are accessed by using this keyword.
  • There name usually starts with an upper case letter.
  • after you create a constructor function all you have to do is to use "new" keyword to create object of that function
Using base Object function:
var obj = new Object();
obj.a= 'val';
obj.b= 'val2';

Using object.create Function:
using obect.Create is supported in major browsers but not in IE6 or 8

var a = Object.create(null, { a: { value: 1 }, b: { value: 2 }}); // no prototype
var b = Object.create({}, { a: { value: "a" }, b: { value: "b" }}); // prototype is an empty obj
var c = Object.create({}, { a: { value: true }, b: { value: false }}); // prototype is an empty obj

console.log(a.a); // outputs 1
console.log(a.b); // outputs 2
console.log(b.a); // outputs a
console.log(b.b); // outputs b
console.log(c.a); // outputs true
console.log(c.b); // outputs false

further read: http://mzl.la/1i0PTnC 
Recommended Books

Node.js on Ubuntu : npm ERR! cb() never called! npm ERR! not ok code 0

While setting up Node.js on Ubuntu I encountered the error below

npm ERR! cb() never called!
npm ERR! not ok code 0

This occurred because when I ran the command

npm install

and stopped it abruptly in between.  The solution that I found was to delete the folder "node_modules" that was created and then run the command below

npm cache clear  

after this I ran the command nmp install again and all was working fine.




Recommended Books:

Popular Posts