SlideShare a Scribd company logo
1 of 45
Fundamentos
JavaScript
Disclaimer
Cualquier similitud con la serie de charlas “Crockford on
JavaScript” es completamente intencional
http://www.yuiblog.com/crockford/
Disclaimer
Agenda
● El lenguaje más incomprendido del mundo
● Un poco de historia
● Características principales
● Literales
● Objects
● Exceptions
● Scope
● Functions
● Prototype
El lenguaje más incomprendido
del mundo
“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
Las librerías y frameworks son abstracciones.
Demasiado buenas?
Buenas abstracciones.
Un mínimo de historia
● Netscape - LiveScript
● Sun - Java
● Microsoft - JScript
● European Computer Manufacturers Association - ECMAScript
JavaScript !==
Java
http://exploringdata.github.io/info/programming-languages-influence-network/
Características principales
● Loose typing
● Funciones como “first class objects”
● Lenguaje basado en prototipos
● Variables globales
Literales
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)
};
Objects
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 };
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]
});
Exceptions
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"
}
function MyError(message) {
this.name = "MyError";
this.message = message || "Default Message";
}MyError.prototype = new Error();MyError.prototype.construct
MyError;try {
throw new MyError();
} catch (e) {
console.log(e.name); // "MyError"
console.log(e.message); // "Default Message"
}
try {
throw {
name: "MyError",
message: "Default Message"
};
} catch (e) {
console.log(e.name); // "MyError"
console.log(e.message); // "Default Message"
}
Scope
function rateFavMovies() {
for (var i = 0; i < favouriteMovies.length; i++) {
var movie = favouriteMovies[i];
movie.rating = 10;
}
...
}
function rateFavMovies() {
var movie;
for (var i = 0; i < favouriteMovies.length; i++) {
movie = favouriteMovies[i];
movie.rating = 10;
}
...
}
function showMovies(query, element) {
movieService.getMovies(query, function (movies) {
renderMovies(movies, element);
});
}
function bookmarker(bookmarks) {
return function (movies) {
for (var i = 0; i < movies.length; i++) {
bookmarks.push(movies[i]);
}
};
};var addToMyBookmarks = bookmarker(myBookmarks);
addToMyBookmarks(someMovies);
function createNotification() {
var status = "Pending";
return {
setStatus: function (newStatus) {
status = newStatus;
},
getStatus: function () {
return status;
}
};
}
var notification = createNotification();
notification.setStatus("Read");
notification.getStatus(); // "Read"
Functions
● Las funciones son first class objects
● Function.prototype
● Unidades básicas de ejecición
function greet() {
console.log("Hi!, I'm " + this.name);
}
greet(); // "Hi!, I'm undefined"
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"
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"
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"
Prototype
message
status
alertUser
var proto = {
alertUser: function () { ... }
};
var notif = Object.create(proto);
notif.message = "Fatal error ...";
notif.status = "Pending";
notif.alertUser();
var theGodfather = {
title: "The Godfather",
director: "Francis Ford Coppola",
cast: ["Marlon Brando", "Al Pacino", "Diane Keaton"]
};
var theGodfather2 = Object.create(theGodfather);
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"
theGodfather.director = "Coppola";
theGodfatehr.director; //"Coppola"
theGodfather2.director; //"Coppola"
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"
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"
Conclusiones
● 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
Fin

More Related Content

What's hot (7)

How To Think In Go
How To Think In GoHow To Think In Go
How To Think In Go
 
Rust Mozlando Tutorial
Rust Mozlando TutorialRust Mozlando Tutorial
Rust Mozlando Tutorial
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularity
 
7 Sins of Java fixed in Kotlin
7 Sins of Java fixed in Kotlin7 Sins of Java fixed in Kotlin
7 Sins of Java fixed in Kotlin
 
Clojure made really really simple
Clojure made really really simpleClojure made really really simple
Clojure made really really simple
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.
 
Mongo
MongoMongo
Mongo
 

Similar to Tech Talks - Fundamentos JavaScript

Python-GTK
Python-GTKPython-GTK
Python-GTK
Yuren Ju
 
Androidの本当にあった怖い話
Androidの本当にあった怖い話Androidの本当にあった怖い話
Androidの本当にあった怖い話
Yusuke Yamamoto
 

Similar to Tech Talks - Fundamentos JavaScript (20)

Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival Guide
 
JavaScript - Like a Box of Chocolates - jsDay
JavaScript - Like a Box of Chocolates - jsDayJavaScript - Like a Box of Chocolates - jsDay
JavaScript - Like a Box of Chocolates - jsDay
 
Why Every Tester Should Learn Ruby
Why Every Tester Should Learn RubyWhy Every Tester Should Learn Ruby
Why Every Tester Should Learn Ruby
 
Python-GTK
Python-GTKPython-GTK
Python-GTK
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascript
 
Learning go for perl programmers
Learning go for perl programmersLearning go for perl programmers
Learning go for perl programmers
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worlds
 
JavaScript - Like a Box of Chocolates
JavaScript - Like a Box of ChocolatesJavaScript - Like a Box of Chocolates
JavaScript - Like a Box of Chocolates
 
Common mistakes in android development
Common mistakes in android developmentCommon mistakes in android development
Common mistakes in android development
 
Androidの本当にあった怖い話
Androidの本当にあった怖い話Androidの本当にあった怖い話
Androidの本当にあった怖い話
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?
 
Building android apps with kotlin
Building android apps with kotlinBuilding android apps with kotlin
Building android apps with kotlin
 
Lightning talk: Go
Lightning talk: GoLightning talk: Go
Lightning talk: Go
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Tech Talks - Fundamentos JavaScript

  • 2. Disclaimer Cualquier similitud con la serie de charlas “Crockford on JavaScript” es completamente intencional http://www.yuiblog.com/crockford/
  • 4. Agenda ● El lenguaje más incomprendido del mundo ● Un poco de historia ● Características principales ● Literales ● Objects ● Exceptions ● Scope ● Functions ● Prototype
  • 5. El lenguaje más incomprendido del mundo
  • 6. “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
  • 7. Las librerías y frameworks son abstracciones. Demasiado buenas? Buenas abstracciones.
  • 8.
  • 9.
  • 10. Un mínimo de historia ● Netscape - LiveScript ● Sun - Java ● Microsoft - JScript ● European Computer Manufacturers Association - ECMAScript
  • 14. ● Loose typing ● Funciones como “first class objects” ● Lenguaje basado en prototipos ● Variables globales
  • 16. 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) };
  • 18. 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 };
  • 19. 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] });
  • 21. 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" }
  • 22. function MyError(message) { this.name = "MyError"; this.message = message || "Default Message"; }MyError.prototype = new Error();MyError.prototype.construct MyError;try { throw new MyError(); } catch (e) { console.log(e.name); // "MyError" console.log(e.message); // "Default Message" }
  • 23. try { throw { name: "MyError", message: "Default Message" }; } catch (e) { console.log(e.name); // "MyError" console.log(e.message); // "Default Message" }
  • 24. Scope
  • 25. function rateFavMovies() { for (var i = 0; i < favouriteMovies.length; i++) { var movie = favouriteMovies[i]; movie.rating = 10; } ... }
  • 26. function rateFavMovies() { var movie; for (var i = 0; i < favouriteMovies.length; i++) { movie = favouriteMovies[i]; movie.rating = 10; } ... }
  • 27. function showMovies(query, element) { movieService.getMovies(query, function (movies) { renderMovies(movies, element); }); }
  • 28. function bookmarker(bookmarks) { return function (movies) { for (var i = 0; i < movies.length; i++) { bookmarks.push(movies[i]); } }; };var addToMyBookmarks = bookmarker(myBookmarks); addToMyBookmarks(someMovies);
  • 29. function createNotification() { var status = "Pending"; return { setStatus: function (newStatus) { status = newStatus; }, getStatus: function () { return status; } }; } var notification = createNotification(); notification.setStatus("Read"); notification.getStatus(); // "Read"
  • 31. ● Las funciones son first class objects ● Function.prototype ● Unidades básicas de ejecición
  • 32. function greet() { console.log("Hi!, I'm " + this.name); } greet(); // "Hi!, I'm undefined"
  • 33. 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"
  • 34. 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"
  • 35. 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"
  • 37. message status alertUser var proto = { alertUser: function () { ... } }; var notif = Object.create(proto); notif.message = "Fatal error ..."; notif.status = "Pending"; notif.alertUser();
  • 38. var theGodfather = { title: "The Godfather", director: "Francis Ford Coppola", cast: ["Marlon Brando", "Al Pacino", "Diane Keaton"] }; var theGodfather2 = Object.create(theGodfather);
  • 39. 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"
  • 40. theGodfather.director = "Coppola"; theGodfatehr.director; //"Coppola" theGodfather2.director; //"Coppola"
  • 41. 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"
  • 42. 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"
  • 44. ● 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
  • 45. Fin