Wednesday, April 20, 2011

Day 1 Last session : Javascript powerful language out there

Last session of the day by Venkat on the power of Javascript

Javascript

Feels like Java & C
Similar to perl in some ways
Untyped Language

Rules

1) Case sensitive
2) goo idea to use ;
3) commenting style is same as Java/C++
4) null and undefined are used to indicated null
5) Strings are single or doule quotes and are immutable

Functional in nature : Functions are first class citizens

Pass and return functions. Everything should be object oriented is a wrong notion.

Variables are typeless. but you need to declare variables with var

Global and local scoping for variables. No block level.

Control structures are like C : statements, expressions, for loops

var sayHello = function(name) {
print('hi');
}

sayHello is now a handle to a function.Its powerful since, you can now register them as event handlers.

IN Javascript everything is an expression. there is no statement as such.

Functions are Objects


function Car(year) {
this.year=year;
}

var myCar = new Car(2011);

print (myCar.year);

myCar is an instance of Car.

Encapsulation is not really about security. Its for code maintainence.

prototype is like a backpack. Any method not defined on object is routed to prototype.

Car.prototype.drive = function(dist) {
print('driving..);
this.miles += dist;
}
Car.prototype.tune = function() {print ('tune')};

print (myCar["year"]); --> you can use this instead of dot notation. You can dynamically recieve the parameter since its in a double quote.

Object is nothing but a collection (property holder).
It can be a function, object or a collection of properties

for (var prop in myCar.__proto__) {
print(prop + '\n');
}


Inheritance in Javascript

Composition is better than Inheritance. Ruby and Groovy have delegation (@delegate).

Javascript supports inheritance through method composition.

function Base() {
}

function Dervied() {
}

Derived.prototype = new Base();

Code Quality

jslint is a nice tool to check code quality.

You need to separate the logic from HTML page.

Overall it was an amazing session with good insights on JS

0 comments: