JavaScript By Rangana Sampath
Good to know • ECMA script specification • JavaScript Engine - ECMAScript Engine • V8 – Chrome , NodeJs , opera after v 15 • Chakra – IE : Jscript • Spider Monkey - Mozilla • Nitro – Apple • Tamarin – Flash : Action Script
How JavaScript work? Source : http://www.quora.com/How-does-a-JavaScript-engine-work
Variables • Case sensitive • Declared using • var (good practice) • without var(can be bad) • Uninitialized (implicit) - undefined • Initialized to nothing (explicit) - null
Operators • Arithmetic operators ( + - / * % ++ -- ) • Assignment operator ( = ) • Compound operators (+= -= /= *= )
Comparison operators
Logical operators
loops • For-in loop • Special loop that can only be used to arrays and objects var a = ['a', 'b', 'c', 'x', 'y', 'z']; var result = 'n'; for (var i in a) { result += 'index: ' + i + ', value: ' + a[i] + 'n'; }
Primitive and Non-primitive Types • Number • String • Boolean • Undefined • Null • Object • Array • Function • Date • RegExp typeof can be used to determine the which type variable belong to - number - boolean - object - String - undefined - function
String • Sequence of characters used to represent text • Concatenation using + sign • Automatic type conversion with arithmetic operators ( except + ) • When conversion fails we get NaN • Can use empty string to convert other types to string • Special strings , escape characters
Numbers • Decimal, octal, hexadecimal, scientific notation • Biggest number 1.7976931348623157e+308 • Smallest number 5e-324 • Infinity , -Infinity • NaN - special type of number , result when assumed numeric operation fails
Boolean • Only have true or false • Double negation?? ( !! ) You can convert any value to true using a double negation except below The empty string "" null undefined The number 0 The number NaN The Boolean false
Arrays • New array can be declared using [] , ex : var x = []; • Have indexed entries from starting 0 • Have properties like length, push, pop, reverse
Functions • If explicitly does not return value implicitly undefined is returned • arguments array in a function • Functions are special kind of data in JavaScript • Anonymous functions – function(a){return a;} • Can be passed as parameters • Can be immediately invoked • Inner / private functions • Function that return functions • Functions that return objects
Variables and scope • Only have function scope • Inside local • Outside global • Using var and not using var again?? • Closure? • When function go out of the scope the variables referenced inside of the function stays as it is • Happens when return type has a reference to inner methods or variables • Hoisting? • When your JavaScript program execution enters a new function, all the variables declared anywhere in the function are moved (or elevated, or hoisted) to the top of the function. • Only the declaration is hoisted. Any assignments stay where they are.
Objects • Arrays vs Objects • var x = [] , var y = {} • Accessing an object's properties • square bracket notation, hero['occupation'] • dot notation, hero.occupation • parent object - Object • The global object (window in browsers, global in nodejs) • Ways to create objects • Object literal notation - {} • With - new Object() • With - Constructor functions
Built in objects • There are three types • Data wrapper objects • Object, Array, Function, Boolean, Number, and String • Utility objects • Math, Date, and RegExp • Error objects
Prototype • Prototype is a property of function object • Constructor function properties vs. prototype properties • Both are same except prototype properties are live and shared • Precedence of function execution? Constructor > prototype • hasOwnProperty , propertyIsEnumerable ,isPrototypeOf methods • Two important concepts • The prototype chain is live except when you completely replace the prototype object • prototype.constructor is not reliable • The secret link of the prototype • When you overwrite the prototype, remember to reset the constructor property.
Inheritance • Prototype chaining • Take care of inheritance before augmenting the prototype • Inheriting prototype only
Code organization • Namespaces • Prototype pattern • Module pattern • Revealing module pattern • Revealing prototype pattern • Other methods and GOF design patterns
Thank you!

Introduction to JavaScript

  • 1.
  • 2.
    Good to know • ECMA script specification • JavaScript Engine - ECMAScript Engine • V8 – Chrome , NodeJs , opera after v 15 • Chakra – IE : Jscript • Spider Monkey - Mozilla • Nitro – Apple • Tamarin – Flash : Action Script
  • 3.
    How JavaScript work? Source : http://www.quora.com/How-does-a-JavaScript-engine-work
  • 4.
    Variables • Casesensitive • Declared using • var (good practice) • without var(can be bad) • Uninitialized (implicit) - undefined • Initialized to nothing (explicit) - null
  • 5.
    Operators • Arithmeticoperators ( + - / * % ++ -- ) • Assignment operator ( = ) • Compound operators (+= -= /= *= )
  • 6.
  • 7.
  • 8.
    loops • For-inloop • Special loop that can only be used to arrays and objects var a = ['a', 'b', 'c', 'x', 'y', 'z']; var result = 'n'; for (var i in a) { result += 'index: ' + i + ', value: ' + a[i] + 'n'; }
  • 9.
    Primitive and Non-primitiveTypes • Number • String • Boolean • Undefined • Null • Object • Array • Function • Date • RegExp typeof can be used to determine the which type variable belong to - number - boolean - object - String - undefined - function
  • 10.
    String • Sequenceof characters used to represent text • Concatenation using + sign • Automatic type conversion with arithmetic operators ( except + ) • When conversion fails we get NaN • Can use empty string to convert other types to string • Special strings , escape characters
  • 11.
    Numbers • Decimal,octal, hexadecimal, scientific notation • Biggest number 1.7976931348623157e+308 • Smallest number 5e-324 • Infinity , -Infinity • NaN - special type of number , result when assumed numeric operation fails
  • 12.
    Boolean • Onlyhave true or false • Double negation?? ( !! ) You can convert any value to true using a double negation except below The empty string "" null undefined The number 0 The number NaN The Boolean false
  • 13.
    Arrays • Newarray can be declared using [] , ex : var x = []; • Have indexed entries from starting 0 • Have properties like length, push, pop, reverse
  • 14.
    Functions • Ifexplicitly does not return value implicitly undefined is returned • arguments array in a function • Functions are special kind of data in JavaScript • Anonymous functions – function(a){return a;} • Can be passed as parameters • Can be immediately invoked • Inner / private functions • Function that return functions • Functions that return objects
  • 15.
    Variables and scope • Only have function scope • Inside local • Outside global • Using var and not using var again?? • Closure? • When function go out of the scope the variables referenced inside of the function stays as it is • Happens when return type has a reference to inner methods or variables • Hoisting? • When your JavaScript program execution enters a new function, all the variables declared anywhere in the function are moved (or elevated, or hoisted) to the top of the function. • Only the declaration is hoisted. Any assignments stay where they are.
  • 16.
    Objects • Arraysvs Objects • var x = [] , var y = {} • Accessing an object's properties • square bracket notation, hero['occupation'] • dot notation, hero.occupation • parent object - Object • The global object (window in browsers, global in nodejs) • Ways to create objects • Object literal notation - {} • With - new Object() • With - Constructor functions
  • 17.
    Built in objects • There are three types • Data wrapper objects • Object, Array, Function, Boolean, Number, and String • Utility objects • Math, Date, and RegExp • Error objects
  • 18.
    Prototype • Prototypeis a property of function object • Constructor function properties vs. prototype properties • Both are same except prototype properties are live and shared • Precedence of function execution? Constructor > prototype • hasOwnProperty , propertyIsEnumerable ,isPrototypeOf methods • Two important concepts • The prototype chain is live except when you completely replace the prototype object • prototype.constructor is not reliable • The secret link of the prototype • When you overwrite the prototype, remember to reset the constructor property.
  • 19.
    Inheritance • Prototypechaining • Take care of inheritance before augmenting the prototype • Inheriting prototype only
  • 20.
    Code organization •Namespaces • Prototype pattern • Module pattern • Revealing module pattern • Revealing prototype pattern • Other methods and GOF design patterns
  • 21.