not within blocks. • What doest the following codes output? var a = 1; if (true) { var a = 2; } alert(a); function foo() { var a = 3; } foo(); alert(a);
variable's scope. (ineffective in global/window scope) var a = 1; var c = 5; function foo() { var a = 3; b = 2; } foo(); alert(a); alert(window.b); alert(window.c);
'foobar'; var aBool = true; var anArr = [1, 2, 'abc']; var aFunc = function(a, b) { return a + b; }; var anObj = {a: '123', b: aFunc, c: anArr}; Use typeof to check their types.
as its name • What happened if your parse a string into an integer ? var a = 'abcd'; document.write(parseInt(a)); • Both NaN and Infinity are number type. (but they cannot be calculated) ◦ Use isNaN function
[] operator to access the properties in an object instance. ◦ Use property string while using []. • How to check if a property is defined under an object? (useful on checking if the library is loaded) ◦ hasOwnProperty method (Use string as the argument) ◦ in operator (also the property string) ◦ check if it's undefined • Delete a property: delete operator.
as a prototype (described later) • Types of definition: ◦ As a variable var aFunc = function(a, b) { .... }; ◦ Normal definition: function aFunc(a, b) { .... } ◦ Anonymous function (function(a, b) { .... })(2, 3);
are almost defined as the following form: ◦ $.get(url, parmas, callback); ◦ $.get(url, callback); • How to use arguments to overload your function? (Hint: tell the types of the function arguments)
function() { var that = this; var btn = document.getElmentById('btn'); btn.addEventListener('click', function(e){ that.val += 1; }, false); } }; (after the button has been clicked...) console.log(obj.val); // 101
in the inner function. (even the parent function has finished) • What happened by following codes? function delayAlert() { var x = 10; setTimeout(function() { alert(x); }, 1000); x = 1000; } delayAlert();
name; this._age = age; }; var peter = new Person('Peter', 20); console.log(peter._name); peter._age++; console.log(peter._age); // 21 var mary = new Person('Mary', 18); console.log(mary._age);
the type of a instance. • The constructor property is a reference to the Object function that creates the instance's prototype. ◦ Not a string, but it can be modified. ◦ instanceof operator • If var peter = new Person('Peter', 20); what's peter.constructor's value?
= function() {}; Authorized.prototype = { post: function() { // do post } }; // Injects Authorized's methods into User class mixin(User, Authorized); var u = new User('eric', 'pass'); u.post(); // ok
a DOM element, and use appendChild to append it into the DOM tree: var dom = document.createElement('div'); dom.id = 'newCreated'; dom.innerHTML = '<h1>Hi!</h1>'; document.body.appendChild(dom); • How to implement a prependChild method?
the DOM element according to a reference element: var ref = document.getElementById('base'); var parent = ref.parentNode; parent.insertBefore(dom, ref); • How to implement an insertAfter method?
the DOM element from to an element: var ref = document.getElementById('base'); var parent = ref.parentNode; parent.removeChild(ref); • How to implement an replaceWith method?
event fires the location change (according to href attribute) ◦ Use preventDefault to prevent firing. ◦ The submit event in form element. aLink.addEventListener('click', function(evt){ // do something ... evt.preventDefault(); }, false);
concatenate strings. [bad] var a = 'Hel'; a += 'lo'; a += ', wor'; a += 'ld!'; [good] var buf = ['Hel']; buf.push('lo'); buf.push(', wor'); buf.push('ld!'); a = buf.join('');
defines many features that make web applications powerful. • 2/3D graphics/games, offline apps, videos/audios.... on Web is possible today (w/o plugins) • Mobile web browsers can utilize the offline, geolocation, ... features to develop interesting apps. • http://whatwg.org/html5
apps. ◦ Integrate with Google Maps API/Services // Tell if the browser support Geolocation API if ("geolocation" in navigator) { navigator.geolocation.getCurrentPosition( succCallback, errCallback, {enableHighAccuracy: true} // options ); } var succCallback = function(data) { var pos = data.coords; console.log(pos.latitude + ", " + pos.longitude); };
the client. ◦ 2 types of storage: local, session ◦ IE8 supports this API // Tell if the browser support WebStorage API if ("localStorage" in window) { var key = 'counter'; // or localStorage.counter or localStorage[key] var counter = localStorage.getItem(key); if (counter === null) { counter = 0; } else { counter++; } // or localStorage.counter = counter or localStorage[key] = counter localStorage.putItem(key, counter); }
data in the client. // Tell if the browser support Web Database API if ("openDatabase" in window) { var db = openDatabase("mydb", "", "My Test Db", 1048576, function(db){ db.changeVersion('', '1.0', function(t){ t.executeSql('CREATE TABLE note (id, name)'); }); }); // execute sql db.transaction(function(t){ t.executeSql('INSERT INTO note(id, name) VALUES(?, ?)', ['1', 'test']); }); }
優良部份 • The JavaScript Programming Language (videos) ◦ http://video.yahoo.com/watch/111593/1710507 • Pro JavaScript Techniques (ISBN: 978-1-59059-727-9) • Pro JavaScript Design Patterns (ISBN: 978-1-59059-908-2) • High Performance Web Sites • Even Faster Web Sites