interactivity to the web First appeared in Netscape 2.0 Second, Internet Explorer 3.0 Industry standard since 1997 Prototype-Oriented language Not compiled; interpreted Version 1.0 released in 1995 3
of objects that form a document • Cross browser and language independent • Allows adding, removing or updating elements on the model • Browsers parse HTML into a DOM tree • Once parsed into objects, each object can be referenced DOM 1 6
var element = document.createElement('h1'); 2 var text = document.createTextNode(‘My Title’); 3 element.appendChild(text); 4 document.body.appendChild(element); 5 element.setAttribute('id', 'myTitle'); 6 var element = document.getElementById('myTitle'); 7 element.textContent = 'Woohoo!!'; My Title http://www.javascript101.com/examples/ Woohoo!! http://www.javascript101.com/examples/ Code Example DOM 1 8
on dynamically-typed languages • Objects are created on run-time either from scratch or by cloning • Variables are not of a certain type, but the values that are stored in them are PROTOTYPE-ORIENTED 3 12
other object • They have properties (as objects) • Can be assigned, passed as arguments, returned and manipulated • Reference to a function can be invoked using the () operator • Can be nested in other functions FUNCTIONS 4 15
with a reference to a function or anonymous function inline • Can canceling the timer before time end • Used for animations, load balancing, validations and timeouts • Breaks code execution top-down order TIMING EVENTS 5 17
a link to the scope chain; local first, then parents and finally - global • Nested functions can use their variables and also their parents • Closure architecture allows a function to carry its scope to another context SCOPES 6 20
var x = 10; // global scope Code Example 2 (function parentScope() { var y = 20; (function currentScope() { var z = 30; console.log(x+y+z); })(); })(); =60 SCOPES 6 22
Mostly implemented in scripting languages • The closured function saves the scope chain of its parent • Allows functions to access their parent scope variables as they were on the moment they were closured • Efficient when looping and using delegate function CLOSURE 7 24
var x = 20; Code Example 2 function outer() { var x = 10; return function inner() { console.log(x); }; }; 3 var returnedFunction = outer(); 4 returnedFunction(); =10 5 function globalFunc() { console.log(x); }; 6 (function(functionArgument) { var x = 10; functionArgument(); })(globalFunc); =20 CLOSURE 7 26
XMLHttpRequest object • Exchange data asynchronously between browser and server • Update page elements without refreshing the page • Data is mostly transferred in JSON format AJAX 10 31
• JavaScript program is parsed and interpreted at run-time • Modern browsers compile parts of the scripts for performance • Interpreting can happen on-load or on-demand • JavaScript engine is contained in the browser layout engine INTERPRETING 37
cookies) • Cannot interact with frames of a different domain • Cannot read the user’s history • Cannot detect user interaction outside the top frame • JavaScript code cannot be hidden or encrypted • Minification and Obfuscation SECURITY 38
20% Use literals vs instantiate 60% 10% 15% 20% For loop vs while loop 0% 0% 0% 0% Cache globals vs uncache globals 70% 40% 2% 0% No try catch vs try catch 90% 17% 0% 96% 39