Object properties
var palestra = new Object;
palestra.titulo = \"JavaScript Everywhere\";
palestra.evento = \"LinuxChix\";
//E, com notação literal:
var palestra = {
titulo: \"JavaScript Everywhere\",
evento: \"LinuxChix\"
};
12
09/18/07
Object properties
//Duas notações para acessar propriedades
palestra.titulo === palestra[\"titulo\"]
//Exceto quando a chave for um número...
var obj = new Object;
obj[0] = \"booo\";
obj[0] === obj.0; //Uops!
13
09/18/07
This, that...
function f() {
print(this); //depende!
}
f(); //this == Global object
18
09/18/07
This, that...
function f() {
print(this); //depende!
}
var obj = new Object;
obj.func = f;
obj.func(); //this == obj
palestra.g = f;
palestra.g(); //this == palestra
19
09/18/07
Herança?
• Toda função é uma construtora de objetos
• Toda função tem uma propriedade chamada “prototype”
• Object, Function, String, Array, etc são funções
• Prototype, por default, aponta para Object.prototype (o objeto
primordial)
20
09/18/07
Anatomia
21
09/18/07
Herdando...
function Pessoa() {};
Function Empregado(){};
Empregado.prototype = new Pessoa();
emp = new Empregado();
22
09/18/07
Extendendo
//Extendendo (sem especializar) classes built in
String.prototype.ultimoCaractere = function() {
return this[this.length1];
}
\"thiago\".ultimoCaractere();
23
09/18/07
Extendendo
Array.prototype.each = function(func) {
for (var i = 0; i < this.length; i++) {
func(this[i])
}
}
[10,7,8].each(function(v) {
print(v); //10, 7, 8
});
24
09/18/07
Closures
function f() {
var local = 99;
return function() {
print(local);
}
}
f()(); //exibe 99
25
09/18/07
Fluent Interfaces
//Versão normótica de um código:
function makeOrder(customer) {
var o1 = new Order();
customer.addOrder(o1);
var video = Video.find(\"The Simpsons\");
o1.addItem(video);
video = Video.find(\"300\");
o1.addItem(video);
video = Video.find(\"Transformers\");
video.setSkippable(true);
o1.addItem(video);
o1.setRush(true);
}
28
09/18/07
Fluent Interfaces
//Nova versão
function makeFluent(customer) {
customer.newOrder().
withVideo(\"The Simpsons\").
withVideo(\"300\").
withVideo(\"Transformers\").skippable().
priorityRush();
}
29
09/18/07
Metaprogramming
• O ato de escrever programas que escrevem programas
• O ato de programar as estruturas da linguagem do programa
30
09/18/07
ECMAScript edition 4
• Fases finais
• \"Compatibilidade\" com a ed. 3
• Microsoft + Mozilla + Adobe + etc + comunidade
• Foco em Programming in the large
• Um zilhão de adições
36
09/18/07
Que adições?
37
09/18/07
Classes
public dynamic class Array extends Object
{
public static CASEINSENSITIVE = 1
private static function toString() {
...
}
function join(sep) {
...
}
}
38
09/18/07
Static typing
(opcional)
var nome : String = “thiago”;
function f(a: int, b: Pessoa) : int {
...
}
39
09/18/07
Type parameters
(generics)
class List<T> {
public add(e: T) { ... }
...
}
var pessoas : List<Pessoa>;
40
09/18/07
Block Expressions
let (x=x+10, y=12) {
print(x+y);
}
print( let (k=k+10, z=12)
(k+z, \"hello, world!\") )
for ( let i=0 ; i < 5 ; i++ )
f(i)
41
09/18/07
Namespaces
namespace debug
namespace release
debug function trace(s) { print(s) }
release function trace(s) { /* do nothing */ }
use namespace debug
trace(\"I will be printed!\")
function f() {
use namespace release
trace(\"I won't be printed!\")
}
42
09/18/07
Packages
package mx.core
{
class UIObject extends ...
}
import acme.core.*
var widget : acme.core.Widget
43
09/18/07
Iterators & Generators
(+ destr. assignment)
function fib() {
let i = 0, j = 1;
while (true) {
yield i;
[i, j] = [j, i + j];
}
}
var g = fib();
for (var i = 0; i < 10; i++)
print(g.next());
44
09/18/07
Alternative
Closure Syntax
function(x)x
function(a,b)[g(),h()]
foo(function(x)x,function(y)y,3)
45
09/18/07
Concluíndo
• JS é uma linguagem que surpreende muita gente
• É pequena, o que a torna sedutora para ser embutida em aplicações
• A nova edição, provavelmente, surpreenderá mais ainda.
51
09/18/07
0 comments
Post a comment