a deeper look into javascript basics

15
A deeper look into Presentation By : Ashutosh Mahto Mindfire Solutions Javascript Basics

Upload: mindfire-solutions

Post on 20-May-2015

421 views

Category:

Technology


1 download

DESCRIPTION

There are several JavaScript libraries available in the world of web programming. And, as the usage and complexity is increasing day by day, sometimes it becomes very difficult and confusing to understand and create modules using those libraries, especially for those having strong background of Object Oriented Languages. So this one hour session will make an effort to go into the very basics of JavaScript and put a base for writing modular JavaScript code.

TRANSCRIPT

Page 1: A Deeper look into Javascript Basics

A deeper look

into

Presentation By :Ashutosh MahtoMindfire Solutions

Javascript

Basics

Page 2: A Deeper look into Javascript Basics

“The world's most misunderstood programming language”

- Douglas Crockford

A Deeper Look Into Javascript Basics

Page 3: A Deeper look into Javascript Basics

Agenda to Discuss

Hoisting in javascript

Scopes In javascript

Objects

Functions

Closures

Prototype

Drawing some basic practices

A Deeper Look Into Javascript Basics

Page 4: A Deeper look into Javascript Basics

Hoisting in Javascript

A Deeper Look Into Javascript Basics

Every declaration, variable declaration or function declaration is hoisted to the top of its declaration context.

/* Hoisiting */var name = "ramesh";function showName() { if (!name) {

var name = "Suresh";

}

console.log(name);

}

showName(); // output - Suresh

Page 5: A Deeper look into Javascript Basics

Defining Scopes in Javascript

- Everything defined without var is global

- Every global variable or object can be accessed through window.<variablename>

- Unlike C, Javascript doesn't have Block Level Scope

- Javascript has Function Level Scope

A Deeper Look Into Javascript Basics

Page 6: A Deeper look into Javascript Basics

Defining Scopes in Javascript

A Deeper Look Into Javascript Basics

#include <stdio.h>int main() {

int x = 1;printf("%d, ", x); // 1if (1) {

int x = 2;printf("%d, ", x); // 2

}printf("%d\n", x); // 1

}

var x = 1;console.log(x); // 1if (true) { var x = 2; console.log(x); // 2}console.log(x); ???

Output – 2, which should be 1 as in C

Scope in C Scope in Javascript

Page 7: A Deeper look into Javascript Basics

Object In Javascript

- Everything except Number, String, Boolean, null and undefined are objects.

- Can be created using object literals or through Object constructor.

- Objects are usually key value pairs.

- Objects can be used as Associative Arrays, but are not actually any type of array.

- Objects can link to another objects through -

var newObject = Object(oldObject);

This will create a new object and keep a link to the Old Object.

If any property is not available in newObject it will be looked into oldObject

A Deeper Look Into Javascript Basics

Page 8: A Deeper look into Javascript Basics

Functions In Javascript

- Executable piece of code, in any programming language.

- In javascript, functions are INVOKABLE objects.

- Like objects functions can carry Properties & Methods, can be copied, deleted and augmented

- Functions can be passed as parameter to other functions and can be returned also

- Functions are First Class Citizens in javascript.

- Every function returns a value, if not, returns undefined.

A Deeper Look Into Javascript Basics

Page 9: A Deeper look into Javascript Basics

Functions in Javascript

- Function declaration

function sayHello() { console.log('Hello!');}

- Function expression

var sayHello = function() { console.log('Hello!');}

- Anonymous Functions

- Self executing functions

(function() { console.log('Hello!');})();

A Deeper Look Into Javascript Basics

Page 10: A Deeper look into Javascript Basics

Closures in Javascript

Local variables for a function are kept alive after the function has returned

A closure is a stack-frame which is not deallocated when the function returns (as if a 'stack-frame' were malloc'ed instead of being on the stack!)

A Deeper Look Into Javascript Basics

var greet = function(time) { var good = "Good"; return function(name){ console.log(good+ " "+ time+ " "+ name); }}var sayGoodMorning = greet("Morning");sayGoodMorning("Suresh");

// Good Morning Suresh

Page 11: A Deeper look into Javascript Basics

Prototype in Javascript

Prototype is the base of Object Oriented Programming in javascript

Every function contains a prototype object that can be chained through its constructor.

A Deeper Look Into Javascript Basics

var Person= function(name) {this.name = name;

}Person.prototype.getName = function() {

return this.name;}var student = new Person("Satish");

Page 12: A Deeper look into Javascript Basics

Drawing some best practices

Start using JSLint

Start using “strict”

Always use var while declaring variables to avoid unnecessary globals

Declare all the variables at the top of its function scope

Maximize the use of function expressions

Always use ; as delimiter

Never think of Objects as a kind of array, and never declare an object as an array.

A Deeper Look Into Javascript Basics

Page 13: A Deeper look into Javascript Basics

Any Question ???

A Deeper Look Into Javascript Basics

Page 14: A Deeper look into Javascript Basics

References and Recommendations Books

Javascript The Good Parts, Douglas Crockford

Javascript: The Definitive Guide, David Flanagan

Professional Javascript For Developers, Nicholas Zakas

Blogs

Articles

Stack Overflow

A Deeper Look Into Javascript Basics

Page 15: A Deeper look into Javascript Basics

Thank You !!!

A Deeper Look Into Javascript Basics