Constructor in JavaScript

Mar 24, 2017

A constructor in JavaScript is a function that is called with the “new” operator.

Categories : JavaScript

Difference between scope and context in JavaScript

Mar 23, 2017

Difference is scope is function based and context is object based.

Scope

Variables can have either local or global scope. Local variables exist only within the function in which they are defined. Variable declared outside the function can be accessed and modified by another function also.

var planet = "sun"; // global variable

console.log(planet); //sun
function solar_system() {
    var star = "sun"; // local variable
}
console.log(star); // Error, star is not defined

Context

Context this is set to the object the function is called on.

var myObj = {
    id: 10,
    myFunc: function() {
        console.log(this.id); // this refers to `myObj`
    }
};

myObj.myFunc(); // 10
function foo() {
    console.log(this); // `this` refers to the `window` object
}

foo();  

Reference

Categories : JavaScript

How to create a class in JavaScript

Mar 22, 2017

In JavaScript classes can be created using functions. Let’s see how.

function Car() {
    this.make = 'Honda';
    this.model = 'Civic';
    this.color = 'gray';

    this.getInfo = function() {
        return this.make + ' ' + this.model + ' ' + this.color;
    };
}
var mycar = new Car();

The method getInfo() gets recreated every time we create a new object. Instead we can add getInfo() to the prototype.

Car.prototype.getInfo = function() {
    return this.make + ' ' + this.model + ' ' + this.color;
}
Categories : OOP   javascript

Welcome to Jekyll!

Mar 22, 2017

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.

Jekyll also offers powerful support for code snippets:

def print_hi(name)
  puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.

Check out the Jekyll docs for more info on how to get the most out of Jekyll. File all bugs/feature requests at Jekyll’s GitHub repo. If you have questions, you can ask them on Jekyll Talk.

Categories : jekyll   update

JavaScript data types, variables, variable scope, literals, hoisting

Jan 1, 2013

Data types

JavaScript has six primitive data types,

undefined - A variable that has not been assigned a value is of type undefined.

null - A special keyword denoting a null value.

Boolean - A logical entity that consists of either a true or false value.

Number - A set of numerical digits that represent a number.

String - A set of zero or more characters.

Symbol - It is a new feature in ECMAScript 2015 (ES6). Symbols are tokens that serve as unique IDs.

Variables

You can use variables as symbolic names for values in application. The names of variables, is called identifiers. Variables can be declared with keyword var.

Variable scope

When you declare variables outside of function , its called global variable. When you declare within a function, its called local variable. JavaScript does not have block statement scope.

Hoisting

You can refer to variable declared later without getting exception. This concept is called “Hoisting”, variables in JavaScript are in a sense “hoisted” or lifted to the top of the function.

Constants

You can create read-only, named constant with const keyword.  A constant cannot change value through assignment.

Literals

These are fixed values, not variables, that you literally provide in you script.

  • Array Literals
var coffees = ["French Roast","Colombian","Kona"];
  • Boolean Literals

The boolean type has two literal values true, false.

  • Object Literals

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces.

var solarSystem = { 
    planets: ['mercury', 'venus', 'neptune', 'mars', 'earth', 'jupiter', 'saturn', 'uranus']
};
  • String literals

A string literal is zero or more characters enclosed in double or single quotation marks.

'foo'
"baz"
  • Template literals

EcmaScript2015(ES6) introduced Template literals. They are text enclosed in back ticks instead of double or single quotes.

`Hello world!`

References

Categories : JavaScript