41. Value vs Reference // copying by Value var a = 1; var b = a; // copy by value.. Two distinct values now // passing an argument by Value function add_to_sum(sum, x) { sum = sum + x; // this only changes the internal } // copy of x add_to_sum(a,1); if (b == 1) b = 2; // b is a distinct numeric value // a is still 1, and b is 2 Won’t actually do anything
42. Value vs Reference // copying by reference var xmas = new Date(2007, 11, 25); var gift_day = xmas; // both variables refer to the same object gift_day.setDate(26); // we’ve now changed both variables function add_to_sum(sum, x) { sum[0] = sum[0] + x; sum[1] = sum[1] + x; sum[2] = sum[1] + x; } (xmas == gift_day) // this is true still xmas.getDate(); // returns 26 not 25 var newdate1 = new Date(2007,10,10); var newdate2 = new Date(2007,10,10); (newdate1 == newdate2) // this is false Permanently changes ‘sum’ in global context Note: Strings are compared by value
51. Operators Operator Operand type(s) Operation performed . Object, identifier Property access [ ] Array, integer Array index ! Boolean Logical complement == Any Equality === Any Identity && Booleans Logical AND || Booleans Logical OR ?: Booleans, any, any Conditional operator , Any Multiple evaluation
52. Assignment with Operation Operator Example Equivalent += a += b a = a + b -= a -= b a = a – b *= a *= b a = a * b /= a /= b a = a / b %= a %= b a = a % b <<= a <<= b a = a <<b >>= a >>= b a = a >>b >>>= a >>>= b a = a >>> b &= a &= b a = a & b |= a |= b a = a | b ^= a ^= b a = a ^ b
53.
54.
55.
56.
57. while, do/while var count = 0; while (count < 10) { console.log(count); count++; } var count = 0; do { console.log(count); count++; } while (count < 10) Will execute at least once
58. for for( initialize ; test ; increment ) statement for (var count = 0; count < 10; count++) console.log(count); for( variable in object ) statement var o = {x:1, y:2, z:3} var a = new Array(); var I = 0; for(a[i++] in o) {} Curly braces {} are not required if just one statement Copies the object “o” to the array “a” Not all properties are enumerable!
89. Exercise 1 – Possible Solution InsertDOM = function() { var myDiv = document.createElement('div'); var myHeading = document.createElement('h2'); myHeading.innerHTML = "Customer Profile"; var myP1 = document.createElement('p'); myP1.innerHTML = "Jimmy Smith"; var myDiv2 = document.createElement('div'); myDiv2.innerHTML = "Jimmy is married with 2 kids and likes to Golf. Favorite beer is Molson Export."; // Here we asseble everything into one node myDiv.appendChild(myHeading); myDiv.appendChild(myP1); myDiv.appendChild(myDiv2); var body = document.getElementsByTagName('body')[0]; body.appendChild(myDiv); }
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103. Exercise 2 – Possible Solution AnimateDOMWithThread = function() { var myDiv = document.createElement('div'); myDiv.style.backgroundColor = "#FFFF00"; myDiv.style.width = "100px"; myDiv.style.height = "100px"; myDiv.innerHTML = "Some text"; var body = document.getElementsByTagName('body')[0]; body.appendChild(myDiv); window.myAnimObj = setTimeout(function() {animateBox(myDiv,100, 100)}, 20); } animateBox = function(myBox, w, h) { myBox.style.width = w + 'px'; myBox.style.height = h + 'px'; var neww = w+1; var newh = h+1; if ((neww <= 300) || (newh <= 300)) window.myAnimObj = setTimeout(function() {animateBox(myBox,neww,newh)}, 20); } Absolute positioning not required
144. DOM Event Decoration var domNode = document.getElementById(“myNode”); domNode.attachEvent(“onmouseover”, highlight); W3C - Firefox var domNode = document.getElementById(“myNode”); domNode.addEventListener(“mouseover”, hightlight, false); Internet Explorer Capture Function pointer Prefixed with “on”
145. Event Object Internet Explorer W3C document.documentElement.clientHeight clientX / Y clientX / Y, pageX / Y clientX / Y returns the event coordinates without the document scroll position taken into account, whereas pageX / Y does take scrolling into account. N/A currentTarget The HTML element to which the event handler was attached. keyCode, altKey, ctrlKey, shiftKey keyCode, altKey, ctrlKey, shiftKey Various key event modifiers to check if ctrlKey, shiftKey ctrlKey, shiftKey the Shift or Ctrl key are pressed. srcElement target The HTML element on which the event actually took place. Both properties are supported in Opera and Safari. type type The event type without the “on” prefix. fromElement / toElement relatedTarget from is used only for mouseover and mouseout events. Both properties are supported in Opera and Safari