This presentation will give you a brief background to JavaScript, what it is and where it comes from. Then it will walk you through general pitfalls, best practices and more advanced topics such as object-orientation, scope and closures.
• Developed byBrendan Eich 1995
• Initially called Mocha, then LiveScript
• First day of light in a beta of Netscape 2
in December 1995
• IE followed suit with JScript in 1996
• JavaScript isone of the world’s most
popular programming languages
• One interpreter on every machine
• ECMAScript standardizing - Fifth edition
• Web browsers becoming much faster
typeof fanClub //"undefined"
var title = "Armageddon";
typeof title // Equals "string"
var age = 37;
typeof age // Equals "number"
41.
function anotherQuote (){
! return "If I ever woke up
with a dead hooker in my
hotel room, Matt would be
the first person I'd call.";
}
typeof anotherQuote; //"function"
42.
var obj ={};
typeof obj = // "object"
var arr = ["B", "E", "N"];
typeof arr // "object"
43.
var obj ={};
typeof obj = // "object"
var arr = ["B", "E", "N"];
typeof arr // "object" ?!
• The prototypechain checks itself first
• It then goes to the parent, parent’s parent
etc...
69.
function Being (){
! this.living = true;
}
Being.prototype.greet = function () {
! return "Hello!";
};
70.
function Ben (){
! this.talks = true;
}
Ben.prototype = new Being;
Ben.prototype.saySomething = function () {
! return "I feel like fame is wasted on me.";
};
71.
// Create aninstance
var ben = new Ben();
// Returns "I feel like fame is wasted on me."
ben.saySomething();
// Returns "Hello!", inherited
// from the Being object
ben.greet();
72.
Checking for thegreet() method in this order:
• ben instance
• Ben prototype
• Being prototype
• Object prototype
73.
• Simple JavaScriptInheritance
• A Base Class for JavaScript Inheritance
• Defining classes and inheritance
Class-based mimicking
74.
• It’s native,i.e. no dependencies
• Freedom of style and version
• Easy readability
• Code handover
Why prototype syntax
75.
“I have beenwriting JavaScript for 8 years
now, and I have never once found need to
use an uber function...
...I now see my early attempts to support the
classical model in JavaScript as a mistake.”
- Douglas Crockford
Why prototype syntax
function outer (){
! function inner () {
! ! return "Inner";
! }
! return inner();
}
outer(); // Accessible
inner(); // Not accessible
83.
“Global scope islike a public toilet.
You can’t avoid going in there. But
try to limit your contact with
surfaces when you do.”
- Dmitry Baranovskiy, Raphaël JS
library
Polluting the global
namespace
• Closures areexpressions, usually functions,
which can work with variables set within a
certain context
• Inner functions referring to local variables
of its outer function create closures
97.
function add (x){
! return function (y) {
! ! return x + y;
! };
}
var add5 = add(5);
var no7 = add5(2); // Equals 7
function add (x){
! return function (y) {
! ! return x + y;
! };
}
var add5 = add(5);
// How JavaScript sees it
var add5 = function (y) {
! return 5 + y;
}
100.
function add (x){
! return function (y) {
! ! return x + y;
! };
}
var add5 = add(5);
// How JavaScript sees it
var add5 = function (y) {
! return 5 + y;
}