a

Friday, 7 August 2015

Understand JavaScript syntax and fundamentals

Function Declaration

Exercise -1
Create a function using declaration

Solution
// Named function declaration function myFunction () { console.log('testing function'); }

Variables

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Function Expression

Exercise -2
Create a function using Expression

Solution

var myFunction = function () { /* logic here */ };

Exercise -3
Assign a function expression to a property

Solution

var myObj = { myFunction: function () { /* logic here */ } };

Exercise -4
Create a function using Expression and invoke same

Solution

var myFunction = function () { /* logic here */ };
myFunction();

Exercise -5

What would be result of following ? 

function changeStuff(a, b, c)
{
  a = a * 10;
  b.item = "changed";
  c = {item: "changed"};
}

var num = 10;
var obj1 = {item: "unchanged"};
var obj2 = {item: "unchanged"};

changeStuff(num, obj1, obj2);

console.log(num);
console.log(obj1.item);    
console.log(obj2.item);


Immediately-invoked function expression (IIFE)

IIFE executes immediately after it’s created.

Exercise -6

Execute an IIFE

Solution

(function () { console.log ('hello I am an IIFE'); })();

Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created. A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.
Exercise -7
Create a closure and Execute
Solution
function init() {var name = "Appcelerator"; // name is a local variable created by init  
function displayName() { // displayName() is the inner function, a closure    
alert(name); // use variable declared in the parent function    
  }
  displayName();    
};
init();
Best Practices


Don't pollute the global scope!

In a JavaScript execution context, all variables are global by default. The only means of scoping variables is to place them inside of a function. (Within a function, if you don't include the var keyword, your variables will be treated as global variables.) A much better approach than global variables/functions is to enclose all your application's API functions and properties into a single variable (namespace). This will minimize the chances of your code colliding with other code or libraries you include in the context later.

Exercise -8
Write a code enclosing all functions and properties into a single variable


Solution
function makeFunc() {
  var name = "Titanium";
  function displayName() {
    alert(name);
  }
  return displayName;
};

var myFunc = makeFunc();
myFunc();


Exercise - 9
Return Inner function from outer function
Solution
function makeFunc() {
  var name = "Titanium";
  function displayName() {
    alert(name);
  }
  return displayName;
};

var myFunc = makeFunc();
myFunc();
b. Identify CommonJS coding patterns
Node.js implements the CommonJS specification for its built-in modules. Knowing how to leverage your own custom modules is very important for structuring your Node.js applications
Exercise - 10
Create a CommonJS Module and Import

Solution

function getMeCoffee(){
}
exports.exposeCoffee=getMeCoffee;
var CoffeeModule=require('coffee');
CoffeeModule.exposeCoffee;

c. Instantiate Titanium objects
Exercise -11
Manipulate properties of a WIndow Object
Solution

blah blah....

d. Describe execution context and the means by which you create one or more
http://www.appcelerator.com/blog/2010/08/execution-contexts/
Exercise - 12

http://docs.appcelerator.com/platform/latest/#!/guide/Coding_Strategies-section-29004891_CodingStrategies-Executioncontexts
e. Identify the benefits of a single-context app design
http://smorgasbork.com/2012/06/25/titanium-mobile-commonjs-fundamentals/
Exercise - 13

Attempt Exception handling

Solution
try{
// do code in try block
 
}
catch(e){
 
alert(e); // if error comes the control comes to catch block.
 
}

Exercise - 14

Call back

Solution

$("#btn_1").click(function() {alert("Btn 1 Clicked");
});

No comments:

Post a Comment