tech talks - fundamentos javascript

45
Fundamentos JavaScript

Post on 17-Oct-2014

312 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Tech Talks - Fundamentos JavaScript

Fundamentos JavaScript

Page 2: Tech Talks - Fundamentos JavaScript

Disclaimer

Cualquier similitud con la serie de charlas “Crockford on

JavaScript” es completamente intencional

http://www.yuiblog.com/

crockford/

Page 3: Tech Talks - Fundamentos JavaScript

Disclaimer

Page 4: Tech Talks - Fundamentos JavaScript

Agenda

● El lenguaje más incomprendido del mundo

● Un poco de historia

● Características principales

● Literales

● Objects

● Exceptions

● Scope

● Functions

● Prototype

Page 5: Tech Talks - Fundamentos JavaScript

El lenguaje más incomprendido del mundo

Page 6: Tech Talks - Fundamentos JavaScript

“I’ve made every mistake that can be made with

JavaScript. The worst one; I didn’t bother to learn the

language first”

- Douglas Crockford

Page 7: Tech Talks - Fundamentos JavaScript

Las librerías y frameworks son abstracciones.

Demasiado buenas?

Buenas abstracciones.

Page 8: Tech Talks - Fundamentos JavaScript
Page 9: Tech Talks - Fundamentos JavaScript
Page 10: Tech Talks - Fundamentos JavaScript

Un mínimo de historia

● Netscape - LiveScript

● Sun - Java

● Microsoft - JScript

● European Computer Manufacturers Association -

ECMAScript

Page 11: Tech Talks - Fundamentos JavaScript

JavaScript !== Java

Page 12: Tech Talks - Fundamentos JavaScript

http://exploringdata.github.io/info/programming-languages-influence-network/

Page 13: Tech Talks - Fundamentos JavaScript

Características principales

Page 14: Tech Talks - Fundamentos JavaScript

● Loose typing

● Funciones como “first class objects”

● Lenguaje basado en prototipos

● Variables globales

Page 15: Tech Talks - Fundamentos JavaScript

Literales

Page 16: Tech Talks - Fundamentos JavaScript

var scarface = {

title: "Scarface",

plot: "Tony Montana manages to leave Cuba during ...",

quotes: [

"Every dog has his day",

"Say hello to my little friend!"

],

releaseDate: new Date(1986, 12, 9)

};

Page 17: Tech Talks - Fundamentos JavaScript

Objects

Page 18: Tech Talks - Fundamentos JavaScript

notification.message; //"Welcome Tony"

notification["message"]; //"Welcome Tony"

notification.type; //undefined

notification.type = "info";

notification.type; //"info"

delete notification.type;

notification.type; //undefined

Object.keys(notification); //["message"]

var notification = { message: "Welcome " + user.name };

Page 19: Tech Talks - Fundamentos JavaScript

movieService.rateMovie(scarface, user, 9,

"Excelent movie!", [aFriend, anotherFriend]);

movieService.rateMovie(scarface, user, 9,

null, [aFriend, anotherFriend]);

movieService.rateMovie({

movie: scarface,

user: user,

rating: 9,

recommendTo: [aFriend, anotherFriend]

});

Page 20: Tech Talks - Fundamentos JavaScript

Exceptions

Page 21: Tech Talks - Fundamentos JavaScript

function coolFunction() {

throw new Error("Not so cool error");

}try {

coolFunction(); //throws Error

} catch (e) {

console.log(e.name); //"Error"

console.log(e.message); //"Not so cool error"

}

Page 22: Tech Talks - Fundamentos JavaScript

function MyError(message) {

this.name = "MyError";

this.message = message || "Default Message";

}MyError.prototype = new Error();MyError.prototype.constructor =

MyError;try {

throw new MyError();

} catch (e) {

console.log(e.name); // "MyError"

console.log(e.message); // "Default Message"

}

Page 23: Tech Talks - Fundamentos JavaScript

try {

throw {

name: "MyError",

message: "Default Message"

};

} catch (e) {

console.log(e.name); // "MyError"

console.log(e.message); // "Default Message"

}

Page 24: Tech Talks - Fundamentos JavaScript

Scope

Page 25: Tech Talks - Fundamentos JavaScript

function rateFavMovies() {

for (var i = 0; i < favouriteMovies.length; i++) {

var movie = favouriteMovies[i];

movie.rating = 10;

}

...

}

Page 26: Tech Talks - Fundamentos JavaScript

function rateFavMovies() {

var movie;

for (var i = 0; i < favouriteMovies.length; i++) {

movie = favouriteMovies[i];

movie.rating = 10;

}

...

}

Page 27: Tech Talks - Fundamentos JavaScript

function showMovies(query, element) {

movieService.getMovies(query, function (movies) {

renderMovies(movies, element);

});

}

Page 28: Tech Talks - Fundamentos JavaScript

function bookmarker(bookmarks) {

return function (movies) {

for (var i = 0; i < movies.length; i++) {

bookmarks.push(movies[i]);

}

};

};var addToMyBookmarks = bookmarker(myBookmarks);

addToMyBookmarks(someMovies);

Page 29: Tech Talks - Fundamentos JavaScript

function createNotification() {

var status = "Pending";

return {

setStatus: function (newStatus) {

status = newStatus;

},

getStatus: function () {

return status;

}

};

}

var notification = createNotification();

notification.setStatus("Read");

notification.getStatus(); // "Read"

Page 30: Tech Talks - Fundamentos JavaScript

Functions

Page 31: Tech Talks - Fundamentos JavaScript

● Las funciones son first class objects

● Function.prototype

● Unidades básicas de ejecición

Page 32: Tech Talks - Fundamentos JavaScript

function greet() {

console.log("Hi!, I'm " + this.name);

}

greet(); // "Hi!, I'm undefined"

Page 33: Tech Talks - Fundamentos JavaScript

function greet() {

console.log("Hi!, I'm " + this.name);

}

var tony = {

name: "Tony Montana",

greet: greet

};

tony.greet(); // "Hi! I'm Tony Montana

greet(); // "Hi!, I'm undefined"

Page 34: Tech Talks - Fundamentos JavaScript

function greet(formal) {

console.log("Hi!, I'm " +

(formal ? "Mr. " : "") + this.name);

}

var tony = {

name: "Tony Montana",

};

tony.greet(); // TypeError

greet.apply(tony); // "Hi! I'm Tony Montana"

greet.apply(tony, [true]); // "Hi! I'm Mr. Tony Montana"

Page 35: Tech Talks - Fundamentos JavaScript

function Greeter(name) {

this.name = name;

}Greeter.prototype.greet = function () {

console.log("Hi! I'm " + this.name);

};

var tonyGreeter = new Greeter("Tony Montana");

tonyGreeter.greet(); // "Hi! I'm Tony Montana"

Page 36: Tech Talks - Fundamentos JavaScript

Prototype

Page 37: Tech Talks - Fundamentos JavaScript

message

status

alertUservar proto = {

alertUser: function () { ... }

};

var notif = Object.create(proto);

notif.message = "Fatal error ...";

notif.status = "Pending";

notif.alertUser();

Page 38: Tech Talks - Fundamentos JavaScript

var theGodfather = {

title: "The Godfather",

director: "Francis Ford Coppola",

cast: ["Marlon Brando", "Al Pacino", "Diane Keaton"]

};

var theGodfather2 = Object.create(theGodfather);

Page 39: Tech Talks - Fundamentos JavaScript

theGodfather2.cast = ["Al Pacino", "Robert De Niro",

"Diane Keaton"];

theGodfather2.title += " Part II";

theGodfather.director; //"Francis Ford Coppola"

theGodfather2.director; //"Francis Ford Coppola"

theGodfather.title; //"The Godfather"

theGodfather2.title; //"The Godfather Part II"

theGodfather.cast; //["M. Brando", "Al Pacino", "D. Keaton"]

theGodfather2.cast;//["Al Pacino", "R. De Niro", "D. Keaton"]

Page 40: Tech Talks - Fundamentos JavaScript

theGodfather.director = "Coppola";

theGodfatehr.director; //"Coppola"

theGodfather2.director; //"Coppola"

Page 41: Tech Talks - Fundamentos JavaScript

function Greeter(name) {

this.name = name;

}Greeter.prototype.greet = function () {

console.log("Hi! I'm " + this.name);

};

var tonyGreeter = new Greeter("Tony Montana");

tonyGreeter.greet(); // "Hi! I'm Tony Montana"

Page 42: Tech Talks - Fundamentos JavaScript

var greeterProto = {

greet: function () {

console.log("Hi! I'm " + this.name);

}

};

var tonyGreeter = Object.create(greeterProto);

tonyGreeter.name = "Tony Montana";

tonyGreeter.greet(); // "Hi! I'm Tony Montana"

Page 43: Tech Talks - Fundamentos JavaScript

Conclusiones

Page 44: Tech Talks - Fundamentos JavaScript

● JavaScript es un lenguaje distinto a los demás

● Hay muchos más secretos por descubrir

● El modelo basado en prototipos ofrece una alternativa

interesante al modelo basado en clases

● Gran expresividad

Page 45: Tech Talks - Fundamentos JavaScript

Fin