4. Topics
Variables & Data types
Object Literals
Functions
Declarations
Functions as arguments
Anonymous functions
5. Variables & Data types
In javascript, variables can hold any type of
data.
Variable types keep on changing as we assign
new data to them.
Code Examples:
var hello = “world”;
hello = 23;
hello = function(){ return “world” }
6. Object Literals
An object literal is a comma separated list of name
value pairs wrapped in curly braces.
In JavaScript an object literal is defined as follows:
var person = {
name: 'James',
age: 28,
isAlive: true
};
7. Objects can have arrays, objects and functions as
their properties
var person = {
name: 'James',
age: 28,
isAlive: true,
childObject: {
property1: „hello world‟,
property2: 4938,
property3: false
},
favoriteColors: [“Blue”, “Yellow”, “Magenta”],
getAge: function(){
return this.age;
}
};
8. Function Declarations
function isNimble() { return true; }
var canFly = function () { return true; };
window.isDeadly = function () { return true; };
Differences:
isNumble is defined at parse-time, whereas canFly and isDeadly is
defined at runtime.
canFly and isDeadly are merely two variables which have anonymous
functions assigned to them whereas isNumble is a named function.
9. Functions as Arguments
function greet( getMessage ) {
var message = “Good “ + getMessage();
alert( message );
}
function eveningGreet () {
return “evening”;
}
greet( eveningGreet );
10. Anonymous Functions
function greet( getMessage ) {
var message = “Good “ + getMessage();
alert( message );
}
greet( function () {
return “evening”;
} );
12. What & Why?
A module is an independent unit of functionality that is part of the total
structure of a web application
it‟s very important to have all the code well-organized
Maintaining a spaghetti-coded application will only cause you headaches
and nightmares
Modularization makes your code easier to manage
13. Benefits
Small pieces of code are separately stored in modules
Easier maintenance
Avoid global namespace problems.
Encapsulation - Expose only selected functions or
properties by wrapping all your code inside a module
14. Module Rules
Modules are like little kids, they need a strict set of rules so
they don’t get into trouble.
Hands to yourself
Only call your own methods or those available in application core.
Don‟t access DOM elements outside of your box
Don‟t access non-native global objects
Don‟t leave your toys around
Don‟t create global objects
Don‟t talk to strangers
Don‟t directly reference other modules
15. LoadRunner
A small javascript dependency manager.
Modules are loaded asynchronously.
24. How AMD works?
Core methods to implement an AMD Module
require – used to get reference to an already loaded module.
define – used to define a module.
define method:
require, exports, module parameters
25. debug module()
define(„ debug„ ,
[ „module‟, „require‟, „exports‟ ] ,
function (module , require, exports) {
function debug( data ) {
console.log( data );
}
module.exports = debug;
}
);
29. Used to merge multiple modules in one single file to reduce number of http
requests.
Code Examples
Example10.html – loading bundles instead of separate modules