Professional Documents
Culture Documents
-- Miško Hevery
Basic Concepts
Browser API
global window
Follow on your computer
obj1.name = "JavaScript";
obj1["name"] ==> "JavaScript"
Object prototype chains
volvo.seats ==> 4
volvo.powered ==> undefined
volvo.__proto__ = vehicle;
volvo.powered ==> true
volvo.powered = 'gasoline';
volvo.powered ==> 'gasoline'
vehicle.powered ==> true
volvo.__proto__.powered ==> true
Defining Functions
function greeter(salutation) {
var counter = 0;
var prefix = '. ' +salutation + ' ';
return function(name) {
counter ++;
return counter + prefix + name + '!';
};
}
var greet = greeter('Hello');
greet('World') ==> '1. Hello World!'
greet('World') ==> '2. Hello World!'
Creating a Class
var Circle = {
area: function(){
return this.radius * this.radius * Math.PI;
}
};
function Circle(radius) {
this.radius = radius;
this.area = function(){
return this.radius * this.radius * Math.PI;
};
}
var instance = {};
Circle.call(instance, 5);
instance.area() ==> 78.5398
var instance2 = new Circle(5);
instance2.area() ==> 78.5398
instance instanceof Circle ==> false
instance2 instanceof Circle ==> true
new Operator
function Class(){}
instance[1|2].constructor == Class
instance[1|2].__proto__ == Class.prototype
instance[1|2] instanceof Class == true
Function constructor prototype
function Circle(radius) {
this.radius = radius;
}
Circle.prototype.area = function() {
return this.radius * this.radius * Math.PI;
};
instance.constructor == Circle
instance.__proto__ == Circle.prototype
instance instanceof Circle == true
Function binding problem
server.getUser(greeter.greet);
// greet function called with wrong 'this'
server.getUser(function(name){
greeter.greet(name);
});
Function Binding
server.getUser(bind(greeter, greeter.greet));
Extending Types
String.prototype.reverse = function(){
var text = '';
for(var i=0; i < this.length; i++)
text = this.charAt(i) + text;
return text;
}
log = '';
for(var i in array) {
log += i + ':' + array[i] + ';';
}
log ==> '0:car;1:book;2:table;'
Object iteration
function greet(){
var salutation = 'Hello';
message = salutation + ' ' + name;
}
window.greet();
window.salutation = undefined;
window.message ==> 'Hello World';
Asynchronous API
// Pseudo Code
while(true) {
var event = waitForEvent();
try{
event();
} catch(e) {}
redrawDOM();
}
DOM
JavaScript Objects
Separate memory heap / Advanced Garbage Collectors
Cycles are not a problem
DOM Objects
C/C++ memory heap / RefCounting: malloc() & free()
Cycles are problem for browser developers
Memory Leak
var jsObject = { element: document.body };
document.body.myAttribute = jsObject;
document.addEventListener(...)
Only problem for long lived AJAX apps (no page reloads)
Further Reading
JavaScript puzzlers
http://ejohn.org/apps/learn/
Reference
http://www.w3schools.com/js/default.asp
http://www.w3schools.com/js/js_ex_dom.asp
Functional Programing
http://eloquentjavascript.net/index.html
JavaScript Libraries
Functional API
http://documentcloud.github.com/underscore/
JavaScript on Server
http://nodejs.org/