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

January 1, 2013

JavaScript has five primitive data types,

  • 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.

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.

String literals

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

References

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals

Advertisements

JavaScript binding, function apply, function call

December 28, 2012

In JavaScript, binding is always explicit, and can be easily lost, so a method using “this” will not refer to the proper object in all situations,  unless you force it to.

JavaScript provides two options to do explicit binding “apply” and “call”.

Apply

Every JavaScript function is equipped with “apply” method that allows you to call the function with specific binding. I takes two arguments, the binding object and an array of arguments to be passed to the function.

fun.apply(thisArg[, argsArray])

Call

“Call” method is similar to “apply”, but it takes the arguments themselves not an array.

fun.call(thisArg[, arg1[, arg2[, ...]]])

References

http://www.alistapart.com/articles/getoutbindingsituations

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/call

http://stackoverflow.com/questions/962033/what-underlies-this-javascript-idiom-var-self-this


JavaScript event delegation

December 27, 2012

JavaScript event delegation is a simple technique by which you add a single event handler to a parent element in order to avoid having to add event handlers to multiple child elements.

Event capturing

Netscape defined an approach called event capturing, where events occur on the highest object in the DOM tree and then work down to the deepest element affected by the event.

Event bubbling

IE defined event bubbling. The deepest element affected by the event should receive the event first , then its parent, etc., until the document object finally receives the event.

W3C DOM level 2 events specification defines both event bubbling and capturing. First the document receives the event, then the capturing phase commences to the most specific element affected by the event. Once the event is handled by the element, it bubbles back up to the document.

Advantages

  • Less event handlers to setup and reside in memory.
  • No need to re-attach handlers after a DOM update.

http://www.nczonline.net/blog/2009/06/30/event-delegation-in-javascript/

http://www.sitepoint.com/javascript-event-delegation-is-easier-than-you-think/


JavaScript private public privileged access

December 26, 2012

Public

The members of an object are all public members. There are two ways for putting members in a new object.

In Constructor

function Container(param) {
    this.member = param;
}

In the prototype

This technique is used to add public methods.

Container.prototype.stamp = function (string) {
    return this.member + string;
}

Private
Private members are made by the constructor. Ordinary vars and parameters of the constructor become the private members.

function Container(param) {
    this.member = param;
    var secret = 3;
    var that = this;
}

Privileged

A privileged method is able to access private methods, variables and is itself accessible to the public method and the outside.  Privileged methods are assigned with “this” within the constructor.

function Container(param) {
    this.member = param;

    this.service = function () {
        return this.member;
    };
}

 

http://javascript.crockford.com/private.html

http://phrogz.net/JS/classes/OOPinJS.html

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript


Object Oriented Programming

December 19, 2012

Object

Object is an instance of a class.  All objects have a state and behavior.

Class

Class is the blueprint from which individual objects are created

Inheritance

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.

Interface

Methods form the object’s interface with the outside world. An interface is a group of related methods with empty bodies.  Interface separates implementation and defines the structure. It is useful when the implementation changes frequently.  Interface forms a contract between the class and the outside world.

Abstract Class

Abstract classes cannot be instantiated.  It can only be used as a super class for other classes that extend the abstract class.  Abstract classes are declared with keyword abstract.  Abstract class methods can have implementations. Abstract class’s methods can’t have implementation only when declared abstract.

Encapsulation

Encapsulation is inclusion within a program object of all the resources needed for the object to function.  It allows class to change its internal implementation without hurting the overall functioning of the system.

Polymorphism

Polymorphism is the ability to request that the same operations be performed by a wide range of different types of things.

Method overloading

Ability to define several methods all with the same name

Method overridding

Subclass overrides a specific implementation of a method that is already provided by one of its super classes.

References

http://docs.oracle.com/javase/tutorial/java/concepts/object.html

http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep#OOP


javascript void(0) for HTML link

May 27, 2012

To create a HTML link which does nothing when user clicks it,  use  like below


<A HREF="javascript:void(0)">Click here to do nothing</A>

When the user clicks the link, void(0) evaluates to undefined, which has no effect in JavaScript.

References –

https://developer.mozilla.org/en/JavaScript/Guide/Expressions_and_Operators#void

Here is a stackoverflow.com link, which answers why  void(0)  is better than using #  for this requirement

http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0


Referencing variables in setTimeout function – Javascript Closures

May 26, 2012

Requirement

– Reference For loop variable i  in setTimeout function

Wrong way

for(var i = 1 ; i <= 10; i++) {
console.log('i in for :: ' + i);
setTimeout(function() {
console.log(' i in timeout :: ' + i);
}, 2000);
}

Ouput

i in for :: 1
i in for :: 2
i in for :: 3
i in for :: 4
i in for :: 5
i in for :: 6
i in for :: 7
i in for :: 8
i in for :: 9
i in for :: 10
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11

Right way using Closures

for(var i = 1 ; i <= 10; i++) {
console.log('i in for :: ' + i);
setTimeout(function(j) {
return function() {
console.log(' j in timeout :: ' + j);
}
}(i), 2000);
}

Ouput

i in for :: 1
i in for :: 2
i in for :: 3
i in for :: 4
i in for :: 5
i in for :: 6
i in for :: 7
i in for :: 8
i in for :: 9
i in for :: 10
j in timeout :: 1
j in timeout :: 2
j in timeout :: 3
j in timeout :: 4
j in timeout :: 5
j in timeout :: 6
j in timeout :: 7
j in timeout :: 8
j in timeout :: 9
j in timeout :: 10

References  –
http://jibbering.com/faq/notes/closures/

https://developer.mozilla.org/en/JavaScript/Guide/Closures