Sunday, November 29, 2015

JavaScript Closures

Variable scope:

  • scoping is how you search for a variable
  • the scope of a variable is the area where that variable can be accessed by name

JavaScript language has lexical scope also known as static scope:

  • JavaScript do variable lookup in the scope in effect when a function is defined: the textual arrangement of its definition
  • functions are executed in the variable scope in effect when they were defined

Nested function closures

  • the nested function's scope chain in effect when the function is defined is still in effect when the function is invoked out of the outer function
  • functions are called closures because they close around their local variables

Example 1: lexical scope for a closure

Thursday, October 15, 2015

JavaScript Function Definition

You can define a function with a:
  • function (definition) expression
  • function (declaration) statement

How to Define/Declare Functions

  • Function Expression
    function [funcName]([arg1, ... [, argn]]){
      statements
    }
    
    • A function expression produces a function object
    • A function expression is like an object literal: in this case it produces an object that inherits from Function.Prototype
  • Function Statement
    function funcName([arg1, ... [, argn]]){
      statements
    }
    
    • A function statement expands into a var statement which declare a variable and assigns a function value to it.
    • Unlike ordinary var statement, both pieces of function statement are hoisted up to the start of script or function

Monday, August 31, 2015

Thursday, August 27, 2015

ECMAScript 5 Array Methods

returned value array methods
undefined array.forEach(function)
a new array array.map(function)
a new array array.filter(predicate)
a boolean value array.every(predicate)
a boolean value array.some(predicate)
a value array.reduce(function[, initialValue])
a value array.reduceRight(function[, initialValue])
index|-1 array.indexOf(searchValue[, fromIndex])
index|-1 array.lastIndexOf(searchValue[, fromIndex])

Saturday, August 22, 2015

JavaScript Array Methods

1. Array Methods


returned valuearray methods
a string array.join([separator])
the array itself array.reverse()
the array itself array.sort([function])
a new array array.concat(value1[, value2[, ...]])
a new array array.slice(startIndex[,endIndex])
deleted elements array array.splice(startIndex, deleteCount[, value1[, value2[, ...]]])
the new length array.push(value1[, value2[, ...]])
the last element array.pop()
the new length array.unshift(value1[, value2[, ...]])
the first element array.shift()
a string array.toString()
a string array.toLocaleString()

Monday, August 10, 2015

JavaScript Arrays

A JavaScript array is an ordered collection of values: a value is called array's element, a value's numeric position is called the element's index.
JavaScript arrays indexes are 32-bits unsigned integer values: the index of the first element is 0, and the highest possible index is (232−2) ≡ 4,294,967,294.
Array values may be of any type and different types can be present in the same array.
JavaScript arrays are dynamic: they grow or shrink according to what is necessary.
A JavaScript array may be dense or sparse:
  • a dense array is an array whose element indexes are contiguous
  • a sparse array is an array whose element indexes are NOT contiguous: it have gaps/holes.
Every JavaScript arrays has a length property:
  • if the array is dense, the length property equals the number of elements and its value is one more than the highest index in the array.
  • if the array is sparse, the length property is greater than the number of elements.

Tuesday, July 14, 2015

ECMAScript 5 Object's Property Setters and Getters

Property Getters and Setters Methods

JavaScript objects have data properties and accessor properties:

  • a data property is a name and a value
  • an accessor property is a method or a pair of methods, known as getter and a setter

How JavaScript programs use accessor properties

  • if a program queries an accessor property the getter method is invoked
  • if a program sets an accessor property the setter method is invoked

How to define an accessor properties with object literals

  • two function declarations where the function keyword is replaced by get or set
  • the function names are the same as the property name
// definition of a read-write accessor property named 'propName'
var object = {   
   get propName() { /* getter method body */ },
   set propName(value) { /* setter method body */ }
};

Tuesday, June 30, 2015

JavaScript Objects

 object ⇔ {prop1, prop2, prop3, ... , propN}
 property ⇔ name:value

A JavaScript object is a collection of properties, each of which has a name and a value. Since property names are strings, we say that an object maps string to values. In computer science, a data structure mapping string to values is named "hash", "hashmap", "hashtable", "dictionary" or "associative array". JavaScript objects are dynamic, as properties can be added and deleted after the object creation.

JavaScript object properties

An object property associates a name to a value. A property name may be any string. The property value may be any JavaScript value: a primitive value, an object, a function or (in ECMAScript5) a getter or a setter function.

Saturday, June 6, 2015

JavaScript Operators

1. Arithmetic Operators

1.1 Basic Arithmetic Operators
The basic arithmetic operators are * (multiplication), / (division), % (modulo: remainder after division), + (addition), and - (subtraction).
division operator
JavaScript do not have integer, all numbers are floating-point. So division operations have floating-point results:
5/2 // 2.5 not 2.
addition operator
The addition operator adds numeric operands or concatenates string operands.
19  +  84  // 103   two numbers -> addition
"19" + "84" // "1984" two strings -> concatenation
In other cases the conversion which takes place gives priority to string concatenation: if either operand is a string or an object that convert to string, the other operand is converted to string and a concatenation is performed.

Saturday, March 21, 2015

Client JavaScript Programs Timeline

An UML state machine diagram is used to show the behaviour of a system. In this case a state machine diagram demonstrates the behaviour of the document object that is its life cycle.


Monday, January 19, 2015

JavaScript toString() and valueOf() methods

toString() and valueOf() for the Object class

Object is the base class for every object in JavaScript. The Object class has two methods toString and valueOf that serve for conversion purpose and every JavaScript object inherits these two methods.
  • the toString() method returns a string representing that object
  • the valueOf() method returns a primitive value representing that object