JavaScript
                            Do you speak it!


Tuesday, August 2, 2011
!"#$%&'(&'&)*
                   *
                   +,&-*.%/012%*3%*4&5#663*
                   78(9::;#%7/<=$&>:"#$%&'(&'&)*




Tuesday, August 2, 2011
“The world’s most

                                  misunderstood

                          programming language”




Tuesday, August 2, 2011
We’ll talk about

                          why JavaScript is the most misunderstood
                          programming language

                          Object Oriented Programming in JavaScript

                          function scope, closures, circular references

                          making the web a little faster



Tuesday, August 2, 2011
JavaScript

                          aka Mocha

                                        aka LiveScript

                          aka JScript
                                              aka ECMAScript




Tuesday, August 2, 2011
Java... Script?


                          somehow related to Java?

                          maybe a subset?

                          less capable interpreted version of Java?




Tuesday, August 2, 2011
Java... Script?


                          developed by Netscape in 1997 (vs. 3.0 in 1999)

                          Java prefix intended to create confusion

                          JavaScript is not interpreted Java




Tuesday, August 2, 2011
Design errors
                                                           1 + “1” = ?
                                                           1 + +”1” = ?
                          overloading “+” means both addition and
                          string concatenation with type conversion

                          error-prone “with” statement

                          reserved word policies are much too strict

                          semicolon insertion was “a huge mistake”



Tuesday, August 2, 2011
Bad implementations


                          earlier implementations were buggy

                          embedded horribly in browsers

                          did not respect the ECMA standard




Tuesday, August 2, 2011
Evolution

                          first versions of JavaScript were quite weak

                          no exception handling, inner functions

                          in its present form, it’s a full OOP language

                          ECMAScript 5.1




Tuesday, August 2, 2011
Overview

                          types and operators, core objects, methods

                          does not have classes, but this functionality is
                          accomplished with object prototypes

                          functions are objects (and can be passed
                          around as parameters)




Tuesday, August 2, 2011
(Loosely) Types
                                                          typeof
                          Number                          instanceof

                          String

                          Boolean

                          Object (Function, Array, Date, RegExp)

                          Null

                          Undefined


Tuesday, August 2, 2011
Lisp in C’s clothing


                          C-like syntax (curly braces, for statement...)

                          appears to be a procedural language

                          it’s actually a functional language!




Tuesday, August 2, 2011
Haskell vs. JS
          palindr :: [Int] -> Bool
          palindr [] = True
          palindr [x] = True
          palindr xs = (head xs == last xs)
            && palindr (tail (init xs))

          function palindr(xs) {
            var i, len = xs.length;
            for (i = 0; i < len / 2; i++)
              if (xs[i] !== xs[len - i - 1])
                return false;

                return true;
          }

Tuesday, August 2, 2011
Objects
                          JavaScript is fundamentally about objects

                          usually implemented as hashtables

                          objects are created using constructors

                          use a namespace for them (don’t modify
                          objects you don’t own)

                          use JS sugar: var obj = {}; and var arr = [];


Tuesday, August 2, 2011
OOP
                                             class Dog {
                                               public string name = “”;
                                               public int yearsOld = 0;
                          polymorphism           void bark(int times);
                                                 void bark(float volume);
                          encapsulation          void sleep();
                                             }
                          inheritance
                                             class Pudel : Dog {
                                               public int cuteness = 5;
                          abstractization    }

                                             Dog pinky = new Dog();
                                             Dog leyla = new Pudel();

Tuesday, August 2, 2011
Objects in JavaScript
          MyNamespace.Dog = function(){
             this.name = “”;
             this.yearsOld = 0;
          };

          MyNamespace.Dog.prototype = {
             bark: function(param){
             },
             sleep: function(){
             }
          };

          var pinky = new Dog();
          var leyla = new Dog();
          leyla.cuteness = 5;

Tuesday, August 2, 2011
Objects in JavaScript
          MyNamespace.Dog = function(){
             this.name = “”;
             this.yearsOld = 0;
          };                              no private members?

          MyNamespace.Dog.prototype = {
             bark: function(param){
             },
             sleep: function(){
             }
          };

          var pinky = new Dog();
          var leyla = new Dog();
          leyla.cuteness = 5;

Tuesday, August 2, 2011
Closures
          function foo(a, b){
            function bar() {
              return a + b;
            }

                return bar();
                                    var res1 = foo(5, 2);
          }
                                    var res2 = foo2(5, 2);
          function foo2(a, b){
                                    var res3 = res2(3);
            function bar(c) {
              return a + b + c;
            }

                return bar;
          }

Tuesday, August 2, 2011
Private members
          MyNamespace.Dog = function(){
            var name = “”;
            var yearsOld = 0;

                this.bark = function(param){
                   if (“Number” === typeof param) {
                   }
                   else {
                   }
                };
                this.sleep = function(){
                };
          };

          var pinky = new Dog();

Tuesday, August 2, 2011
Private members
          MyNamespace.Dog = function(paramName, paramYearsOld){
            var name = paramName;
            var yearsOld = paramYearsOld;

                this.bark = function(param){
                };
                this.sleep = function(){
                };

                get nrYearsOld(){
                   return yearsOld;
                };
          };

          var pinky = new Dog();
          console.log(pinky.nrYearsOld);
Tuesday, August 2, 2011
Why is JavaScript
                                slow?


                           Bad compilation?



Tuesday, August 2, 2011
Why is JavaScript
                                slow?


                           No compilation!*



Tuesday, August 2, 2011
Why is JavaScript
                                slow?


                           No compilation!*

             *yes, interpreters are a lot smarter these days, but..

Tuesday, August 2, 2011
Scope chains
          function scope1(){
            var elem2;

                function scope2(){
                  var elem2;

                      function scope3(){
                        var elem3;
                        var func = window.alert;

                          return {
                            something: elem4,
                            somethingElse: elem5
                          }
                      }
                }
          }
Tuesday, August 2, 2011
Memory leaks
          function leakMemory(){
            var elem = somethingThatOccupiesLotsOfMemory;

                function inner(){
                };

                return inner;
          }

          var res1 = leakMemory();
          var res2 = leakMemory();



Tuesday, August 2, 2011
Circular references
          function leakMemory(){
            var elem = document.getElementById(“someId”);

                elem.onclick = function(){
                };
          }




Tuesday, August 2, 2011
Circular references
          function dontLeakMemory(){
            var elem = document.getElementById(“someId”);

                elem.onclick = function(){
                };

                elem = null;
          }




Tuesday, August 2, 2011
Circular references
          function leakMemory(){
            var elem = document.getElementById(“someId”);

                elem.onclick = function(){
                };

                return elem;
          }




Tuesday, August 2, 2011
Circular references
          function dontLeakMemory(){
            var elem = document.getElementById(“someId”);

                elem.onclick = function(){
                };

                try {
                  return elem;
                }
                finally {
                  elem = null;
                }
          }

Tuesday, August 2, 2011
Scope binding
          function scopeBinding(){
            this.elem = {}

                function addMemberToElem(){
                   this.elem.newProperty = 5; // fail
                };

                addMemberToElem();
          }




Tuesday, August 2, 2011
Scope binding
          function scopeBinding(){
            this.elem = {};

                function addMemberToElem(){
                   this.elem.newProperty = 5;
                };

                addMemberToElem.call(this);
          }




Tuesday, August 2, 2011
Scope binding
          function scopeBinding(){
            this.elem = {}

                function addMemberToElem(propertyValue){
                   this.elem.newProperty = propertyValue;
                };

                addMemberToElem.call(this, 5);
          }




Tuesday, August 2, 2011
Scope binding
          function scopeBinding(){
            this.elem = {}

                var addMemberToElem = function(propertyValue){
                  this.elem.newProperty = propertyValue;
                }.bind(this);

                addMemberToElem(5);
          }




Tuesday, August 2, 2011
“But why should I do OOP
                             with JavaScript?”

Tuesday, August 2, 2011
“But why should I do OOP
                             with JavaScript?”

Tuesday, August 2, 2011
?
Tuesday, August 2, 2011

More Related Content

PDF
Developing a Language
PPTX
Javascript Prototype Visualized
PDF
обзор Python
PDF
Developing a Language
PDF
The Sincerest Form of Flattery
PPT
Scala - brief intro
PPTX
Programming picaresque
PPT
Scala uma poderosa linguagem para a jvm
Developing a Language
Javascript Prototype Visualized
обзор Python
Developing a Language
The Sincerest Form of Flattery
Scala - brief intro
Programming picaresque
Scala uma poderosa linguagem para a jvm

What's hot (20)

PDF
Workshop Scala
PDF
JavaScript 101
PDF
NLJUG University Sessie: Java Reborn, Powered by Ordina
PDF
Javascript basic course
PDF
7 Sins of Java fixed in Kotlin
PPT
Java tutorial
PPT
Advanced JavaScript
PPTX
모던자바의 역습
PDF
scala
PDF
Google Guava & EMF @ GTUG Nantes
ODP
PPTX
Advanced JavaScript
PDF
Twins: Object Oriented Programming and Functional Programming
PPTX
Looping the Loop with SPL Iterators
PPT
Basic Javascript
PDF
Why we cannot ignore Functional Programming
PPT
Advanced Javascript
PDF
Functional Programming in Java 8 - Exploiting Lambdas
PDF
Lambda and Stream Master class - part 1
PPTX
Java 8 presentation
Workshop Scala
JavaScript 101
NLJUG University Sessie: Java Reborn, Powered by Ordina
Javascript basic course
7 Sins of Java fixed in Kotlin
Java tutorial
Advanced JavaScript
모던자바의 역습
scala
Google Guava & EMF @ GTUG Nantes
Advanced JavaScript
Twins: Object Oriented Programming and Functional Programming
Looping the Loop with SPL Iterators
Basic Javascript
Why we cannot ignore Functional Programming
Advanced Javascript
Functional Programming in Java 8 - Exploiting Lambdas
Lambda and Stream Master class - part 1
Java 8 presentation
Ad

Viewers also liked (6)

PDF
De la validarea formularelor Web la porcii verzi (utilizarea automatelor in c...
KEY
[TW] Node.js
PDF
Pagini web mai rapide
PPT
Frontline 2.3
PPT
Alec Williams - Author Allies, Poet Partners and Story Stars: Working with Cr...
 
PPTX
JPSSE - Partido State University
De la validarea formularelor Web la porcii verzi (utilizarea automatelor in c...
[TW] Node.js
Pagini web mai rapide
Frontline 2.3
Alec Williams - Author Allies, Poet Partners and Story Stars: Working with Cr...
 
JPSSE - Partido State University
Ad

Similar to Javascript, Do you speak it! (20)

PDF
A Re-Introduction to JavaScript
PDF
javascript teach
PDF
JSBootcamp_White
PDF
Javascript Basics - part 1
PDF
JSLent: give it up for JavaScript
PDF
Tamarin and ECMAScript 4
PDF
Understanding JavaScript
PDF
Javascript - Tutorial
PDF
03 Advanced JavaScript
PDF
JavaScript 1.5 to 2.0 (TomTom)
PDF
Tamarin And Ecmascript 4
PDF
The Future of JavaScript (Ajax Exp '07)
KEY
Object Oriented Programming in js
PDF
The Ceylon Type System - Gavin King presentation at QCon Beijing 2011
PDF
The Ceylon Type System
PDF
JavaScript for PHP Developers
PDF
DEVCON1 - BooJs
PDF
Js in the open
PPTX
An Introduction to JavaScript
PDF
JavaScript Intro
A Re-Introduction to JavaScript
javascript teach
JSBootcamp_White
Javascript Basics - part 1
JSLent: give it up for JavaScript
Tamarin and ECMAScript 4
Understanding JavaScript
Javascript - Tutorial
03 Advanced JavaScript
JavaScript 1.5 to 2.0 (TomTom)
Tamarin And Ecmascript 4
The Future of JavaScript (Ajax Exp '07)
Object Oriented Programming in js
The Ceylon Type System - Gavin King presentation at QCon Beijing 2011
The Ceylon Type System
JavaScript for PHP Developers
DEVCON1 - BooJs
Js in the open
An Introduction to JavaScript
JavaScript Intro

More from Victor Porof (10)

PDF
Firefox WebGL developer tools
PDF
Firefox developer tools
PDF
Processing.js vs. three.js
PDF
Cityquest - Developing games for the mobile devices
PDF
Web3D - Semantic standards, WebGL, HCI
PDF
Chameleon game engine
PPTX
Developing web apps using Java and the Play framework
PPTX
Beginners' guide to Ruby on Rails
PPTX
Introduction to the XNA framework
PPT
Introduction to 3D and shaders
Firefox WebGL developer tools
Firefox developer tools
Processing.js vs. three.js
Cityquest - Developing games for the mobile devices
Web3D - Semantic standards, WebGL, HCI
Chameleon game engine
Developing web apps using Java and the Play framework
Beginners' guide to Ruby on Rails
Introduction to the XNA framework
Introduction to 3D and shaders

Recently uploaded (20)

PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Getting Started with Data Integration: FME Form 101
PDF
Architecture types and enterprise applications.pdf
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
CloudStack 4.21: First Look Webinar slides
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
August Patch Tuesday
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
The various Industrial Revolutions .pptx
PPTX
Modernising the Digital Integration Hub
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
Hindi spoken digit analysis for native and non-native speakers
Developing a website for English-speaking practice to English as a foreign la...
Benefits of Physical activity for teenagers.pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
WOOl fibre morphology and structure.pdf for textiles
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Web Crawler for Trend Tracking Gen Z Insights.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
Getting Started with Data Integration: FME Form 101
Architecture types and enterprise applications.pdf
A contest of sentiment analysis: k-nearest neighbor versus neural network
CloudStack 4.21: First Look Webinar slides
sustainability-14-14877-v2.pddhzftheheeeee
Taming the Chaos: How to Turn Unstructured Data into Decisions
August Patch Tuesday
Module 1.ppt Iot fundamentals and Architecture
The various Industrial Revolutions .pptx
Modernising the Digital Integration Hub
Univ-Connecticut-ChatGPT-Presentaion.pdf

Javascript, Do you speak it!

  • 1. JavaScript Do you speak it! Tuesday, August 2, 2011
  • 2. !"#$%&'(&'&)* * +,&-*.%/012%*3%*4&5#663* 78(9::;#%7/<=$&>:"#$%&'(&'&)* Tuesday, August 2, 2011
  • 3. “The world’s most misunderstood programming language” Tuesday, August 2, 2011
  • 4. We’ll talk about why JavaScript is the most misunderstood programming language Object Oriented Programming in JavaScript function scope, closures, circular references making the web a little faster Tuesday, August 2, 2011
  • 5. JavaScript aka Mocha aka LiveScript aka JScript aka ECMAScript Tuesday, August 2, 2011
  • 6. Java... Script? somehow related to Java? maybe a subset? less capable interpreted version of Java? Tuesday, August 2, 2011
  • 7. Java... Script? developed by Netscape in 1997 (vs. 3.0 in 1999) Java prefix intended to create confusion JavaScript is not interpreted Java Tuesday, August 2, 2011
  • 8. Design errors 1 + “1” = ? 1 + +”1” = ? overloading “+” means both addition and string concatenation with type conversion error-prone “with” statement reserved word policies are much too strict semicolon insertion was “a huge mistake” Tuesday, August 2, 2011
  • 9. Bad implementations earlier implementations were buggy embedded horribly in browsers did not respect the ECMA standard Tuesday, August 2, 2011
  • 10. Evolution first versions of JavaScript were quite weak no exception handling, inner functions in its present form, it’s a full OOP language ECMAScript 5.1 Tuesday, August 2, 2011
  • 11. Overview types and operators, core objects, methods does not have classes, but this functionality is accomplished with object prototypes functions are objects (and can be passed around as parameters) Tuesday, August 2, 2011
  • 12. (Loosely) Types typeof Number instanceof String Boolean Object (Function, Array, Date, RegExp) Null Undefined Tuesday, August 2, 2011
  • 13. Lisp in C’s clothing C-like syntax (curly braces, for statement...) appears to be a procedural language it’s actually a functional language! Tuesday, August 2, 2011
  • 14. Haskell vs. JS palindr :: [Int] -> Bool palindr [] = True palindr [x] = True palindr xs = (head xs == last xs) && palindr (tail (init xs)) function palindr(xs) { var i, len = xs.length; for (i = 0; i < len / 2; i++) if (xs[i] !== xs[len - i - 1]) return false; return true; } Tuesday, August 2, 2011
  • 15. Objects JavaScript is fundamentally about objects usually implemented as hashtables objects are created using constructors use a namespace for them (don’t modify objects you don’t own) use JS sugar: var obj = {}; and var arr = []; Tuesday, August 2, 2011
  • 16. OOP class Dog { public string name = “”; public int yearsOld = 0; polymorphism void bark(int times); void bark(float volume); encapsulation void sleep(); } inheritance class Pudel : Dog { public int cuteness = 5; abstractization } Dog pinky = new Dog(); Dog leyla = new Pudel(); Tuesday, August 2, 2011
  • 17. Objects in JavaScript MyNamespace.Dog = function(){ this.name = “”; this.yearsOld = 0; }; MyNamespace.Dog.prototype = { bark: function(param){ }, sleep: function(){ } }; var pinky = new Dog(); var leyla = new Dog(); leyla.cuteness = 5; Tuesday, August 2, 2011
  • 18. Objects in JavaScript MyNamespace.Dog = function(){ this.name = “”; this.yearsOld = 0; }; no private members? MyNamespace.Dog.prototype = { bark: function(param){ }, sleep: function(){ } }; var pinky = new Dog(); var leyla = new Dog(); leyla.cuteness = 5; Tuesday, August 2, 2011
  • 19. Closures function foo(a, b){ function bar() { return a + b; } return bar(); var res1 = foo(5, 2); } var res2 = foo2(5, 2); function foo2(a, b){ var res3 = res2(3); function bar(c) { return a + b + c; } return bar; } Tuesday, August 2, 2011
  • 20. Private members MyNamespace.Dog = function(){ var name = “”; var yearsOld = 0; this.bark = function(param){ if (“Number” === typeof param) { } else { } }; this.sleep = function(){ }; }; var pinky = new Dog(); Tuesday, August 2, 2011
  • 21. Private members MyNamespace.Dog = function(paramName, paramYearsOld){ var name = paramName; var yearsOld = paramYearsOld; this.bark = function(param){ }; this.sleep = function(){ }; get nrYearsOld(){ return yearsOld; }; }; var pinky = new Dog(); console.log(pinky.nrYearsOld); Tuesday, August 2, 2011
  • 22. Why is JavaScript slow? Bad compilation? Tuesday, August 2, 2011
  • 23. Why is JavaScript slow? No compilation!* Tuesday, August 2, 2011
  • 24. Why is JavaScript slow? No compilation!* *yes, interpreters are a lot smarter these days, but.. Tuesday, August 2, 2011
  • 25. Scope chains function scope1(){ var elem2; function scope2(){ var elem2; function scope3(){ var elem3; var func = window.alert; return { something: elem4, somethingElse: elem5 } } } } Tuesday, August 2, 2011
  • 26. Memory leaks function leakMemory(){ var elem = somethingThatOccupiesLotsOfMemory; function inner(){ }; return inner; } var res1 = leakMemory(); var res2 = leakMemory(); Tuesday, August 2, 2011
  • 27. Circular references function leakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ }; } Tuesday, August 2, 2011
  • 28. Circular references function dontLeakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ }; elem = null; } Tuesday, August 2, 2011
  • 29. Circular references function leakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ }; return elem; } Tuesday, August 2, 2011
  • 30. Circular references function dontLeakMemory(){ var elem = document.getElementById(“someId”); elem.onclick = function(){ }; try { return elem; } finally { elem = null; } } Tuesday, August 2, 2011
  • 31. Scope binding function scopeBinding(){ this.elem = {} function addMemberToElem(){ this.elem.newProperty = 5; // fail }; addMemberToElem(); } Tuesday, August 2, 2011
  • 32. Scope binding function scopeBinding(){ this.elem = {}; function addMemberToElem(){ this.elem.newProperty = 5; }; addMemberToElem.call(this); } Tuesday, August 2, 2011
  • 33. Scope binding function scopeBinding(){ this.elem = {} function addMemberToElem(propertyValue){ this.elem.newProperty = propertyValue; }; addMemberToElem.call(this, 5); } Tuesday, August 2, 2011
  • 34. Scope binding function scopeBinding(){ this.elem = {} var addMemberToElem = function(propertyValue){ this.elem.newProperty = propertyValue; }.bind(this); addMemberToElem(5); } Tuesday, August 2, 2011
  • 35. “But why should I do OOP with JavaScript?” Tuesday, August 2, 2011
  • 36. “But why should I do OOP with JavaScript?” Tuesday, August 2, 2011