SlideShare a Scribd company logo
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
JavaScript
The good, bad and awful parts
Moamen Mokhtar Usama Elnily
September 17, 2013
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Brief History
Facts about JS
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Brief History
Facts about JS
What is Javascript
The world most missunderstood programming language
(Neither Java nor Script!)
Lots of design errors.
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Brief History
Facts about JS
History
In 1992, James Gosling and java.
Netscape (Berndan Eich) and LiveScript.
Sun, Netscape and JavaScript.
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Brief History
Facts about JS
Javascript the language
Load and go delivery.
Loose typing.
Objects as general containers.
Prototypal inheritance.
Lambda.
Linkage through global variables.
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Brief History
Facts about JS
Facts about JS
NaN
NaN === NaN
==
’1’ == 1
” == false
typeof
typeof ”help”.toString;
for .. in
obj.
obj[’ ’]
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Function definition
var foo = function();
function foo ();
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Augmenting
Function.prototype.method = function(name , func)
{
this.prototype[name] = func;
return this;
};
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Scope I
No block Scope.
Hoisting.
foo (); // not visible
bar (); // visible
var foo = function (){
...
};
function bar (){
...
};
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Scope II
function foo (){
function bar() {
return 3;
}
return bar ();
function bar() {
return 8;
}
}
alert(foo ());
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Scope III
function foo (){
var bar = function () {
return 3;
};
return bar ();
var bar = function () {
return 8;
};
}
alert(foo ());
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Scope IV
alert(foo ());
function foo (){
var bar = function () {
return 3;
};
return bar ();
var bar = function () {
return 8;
};
}
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Scope V
function foo (){
return bar ();
var bar = function () {
return 3;
};
var bar = function () {
return 8;
};
}
alert(foo ());
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Scope VI
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar ();
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Arguments
An array-like variable.
Contains all of the arguments of the functions.
Doesn’t have all the functions of arrays except length().
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Apply and Call function invocation
Difference between apply and call.
theFunction.apply(valueForThis , arrayOfArgs)
theFunction.call(valueForThis , arg1 , arg2 , ...)
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Closure
var myObject = function ( ) {
var value = 0;
return {
increment: function (inc) {
value += typeof inc === ’number ’ ? inc : 1;
},
getValue: function ( ) {
return value;
}
}
}( );
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Curry
Function.method(’curry ’, function ( ) {
var slice = Array.prototype.slice ,
args = slice.apply(arguments),
that = this;
return function ( ) {
return that.apply(null ,
args.concat(slice.apply(arguments )));
};
});
Creates closure to hold arguments of the outer function to be
used in the inner function.
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Memoization I
var memoizer = function (memo , fundamental) {
var shell = function (n) {
var result = memo[n];
if (typeof result !== ’number ’) {
result = fundamental(shell , n);
memo[n] = result;
}
return result;
};
return shell;
};
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Function definition
Augmenting
Scope
Arguments
Apply vs. Call
Closure
Curry
Memoization
Memoization II
var fibonacci = memoizer ([0, 1],
function (shell , n) {
return shell(n - 1) + shell(n - 2);
});
var factorial = memoizer ([1, 1],
function (shell , n) {
return n * shell(n - 1);
});
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Important methods
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Important methods
Important methods
array.slice(start, end )
It makes a copy of a portion of the array starting from start to
end.
array.splice(start, deleteCount, item...)
The splice method removes elements from an array, replacing
them with new items.
It is different than delete as delete will replace the deleted
value with undefined
array.sort(comparefn )
The default sort function converts the array entries into strings
and sort lexicographically!.
It is provided with the sorting function (like comparable
objects in java).
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
Using async. functions
Prefered with discontinous events to prevent frozen clients.
What is a callback?
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
for(vari =1;i <=3;i++){
setTimeout(function (){ console.log(i);} ,0);
};
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
The devil is in the details
Javascript is single threaded.
Meet the queue.
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
Sometimes-async functions I
Functions that are async sometimes, but not at other times.
Example: Async functions with caching.
var socketsCache ={};
function openWebSocket(serverAddress ,callback ){
var socket;
if(serverAddress in webSocketCache ){
socket=webSocketCache [serverAddress ];
if(socket.readyState === WebSocket.OPEN ){
callback ();
}
....
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
Sometimes-async functions II
}else{
socket=newWebSocket(serverAddress );
webSocketCache [serverAddress ]= socket;
....
};
return socket;
};
var socket=openWebSocket(url ,function (){
....
});
....
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Why using async. functions
Sometimes-async functions
Throwing errors from callbacks
Throwing errors from callbacks
Can we catch error thrown from a callabck ?
try{
setTimeout(function (){
throw new Error(’Catch_me_if_you_can !’);
},0);
}catch(e){
console.error(e);
}
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Awful and bad parts I
global variables
A non-declared variable is considered global.
Semicolon insertion
return
{
status: true
};
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
Awful and bad parts II
Reserved Words
Use them within quotes.
Access with subscript
object = {case: value }; // illegal
object = {’case ’: value }; // ok
object.case = value; // illegal
object[’case ’] = value; // ok
Bitwise Operators
Very slow!
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
References
Outline
1 Intro
2 Functions
3 Arrays
4 Asyncrounus Javascript
5 Awful and bad parts
6 References
Moamen Mokhtar, Usama Elnily JavaScript
Intro
Functions
Arrays
Asyncrounus Javascript
Awful and bad parts
References
References
References
Douglas Crockford.
JavaScript: The Good Parts.
O’Reilly, 2008.
Trevor Burnham.
Async JavaScript.
The Pragmatic Programmers, 2012.
Moamen Mokhtar, Usama Elnily JavaScript

More Related Content

KEY
Aprendendo solid com exemplos
PDF
Dusting the globe: analysis of NASA World Wind project
PPTX
Journey of a C# developer into Javascript
PDF
C++ boot camp part 1/2
PDF
Errors detected in the Visual C++ 2012 libraries
PDF
Top 10 C# projects errors found in 2016
PDF
Promises - Asynchronous Control Flow
PDF
A Spin-off: CryEngine 3 SDK Checked with CppCat
Aprendendo solid com exemplos
Dusting the globe: analysis of NASA World Wind project
Journey of a C# developer into Javascript
C++ boot camp part 1/2
Errors detected in the Visual C++ 2012 libraries
Top 10 C# projects errors found in 2016
Promises - Asynchronous Control Flow
A Spin-off: CryEngine 3 SDK Checked with CppCat

What's hot (20)

PDF
CppCat Checks OpenMW: Not All is Fine in the Morrowind Universe
PDF
Serious Sam shooter anniversary - finding bugs in the code of the Serious Eng...
PDF
PVS-Studio team is about to produce a technical breakthrough, but for now let...
PDF
From Elixir to Akka (and back) - ElixirConf Mx 2017
PDF
Ad-hoc Runtime Object Structure Visualizations with MetaLinks
PDF
The Unicorn's Travel to the Microcosm
PDF
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
PDF
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
PDF
Analysis of the Trans-Proteomic Pipeline (TPP) project
PDF
A Spin-off: Firebird Checked by PVS-Studio
PDF
PVS-Studio is there to help CERN: analysis of Geant4 project
PDF
Python and Ruby implementations compared by the error density
PDF
ChakraCore: analysis of JavaScript-engine for Microsoft Edge
PPTX
Functional programming for production quality code
PDF
Re-analysis of Umbraco code
PDF
Denis Lebedev, Swift
PDF
Checking the Cross-Platform Framework Cocos2d-x
PDF
PVS-Studio vs Chromium - Continuation
PDF
__array_function__ conceptual design & related concepts
PPTX
Navigating the wild seas of es6 modules
CppCat Checks OpenMW: Not All is Fine in the Morrowind Universe
Serious Sam shooter anniversary - finding bugs in the code of the Serious Eng...
PVS-Studio team is about to produce a technical breakthrough, but for now let...
From Elixir to Akka (and back) - ElixirConf Mx 2017
Ad-hoc Runtime Object Structure Visualizations with MetaLinks
The Unicorn's Travel to the Microcosm
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Analysis of the Trans-Proteomic Pipeline (TPP) project
A Spin-off: Firebird Checked by PVS-Studio
PVS-Studio is there to help CERN: analysis of Geant4 project
Python and Ruby implementations compared by the error density
ChakraCore: analysis of JavaScript-engine for Microsoft Edge
Functional programming for production quality code
Re-analysis of Umbraco code
Denis Lebedev, Swift
Checking the Cross-Platform Framework Cocos2d-x
PVS-Studio vs Chromium - Continuation
__array_function__ conceptual design & related concepts
Navigating the wild seas of es6 modules
Ad

Similar to Java script good_bad_awful (20)

PPTX
Intro to Javascript
PDF
Rediscovering JavaScript: The Language Behind The Libraries
PPT
A Deeper look into Javascript Basics
PDF
JavaScript: The Good Parts
PDF
Javascript good parts - for novice programmers
KEY
JavaScript @ CTK
PPT
JavaScript - Programming Languages course
PDF
Js in-ten-minutes
PPTX
Javascript
PDF
JavaScript and HTML5 - Brave New World (revised)
PPTX
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
PPT
Goodparts
ODP
jsbasics-slide
PDF
Let's JavaScript
PDF
JavaScript Basics and Best Practices - CC FE & UX
PDF
Functional JavaScript Fundamentals
KEY
Underscore.js
PPTX
Lecture 5: Client Side Programming 1
PPTX
JavaScript Fundamentals & JQuery
KEY
JavaScript Neednt Hurt - JavaBin talk
Intro to Javascript
Rediscovering JavaScript: The Language Behind The Libraries
A Deeper look into Javascript Basics
JavaScript: The Good Parts
Javascript good parts - for novice programmers
JavaScript @ CTK
JavaScript - Programming Languages course
Js in-ten-minutes
Javascript
JavaScript and HTML5 - Brave New World (revised)
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
Goodparts
jsbasics-slide
Let's JavaScript
JavaScript Basics and Best Practices - CC FE & UX
Functional JavaScript Fundamentals
Underscore.js
Lecture 5: Client Side Programming 1
JavaScript Fundamentals & JQuery
JavaScript Neednt Hurt - JavaBin talk
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
Tartificialntelligence_presentation.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Spectroscopy.pptx food analysis technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Big Data Technologies - Introduction.pptx
Tartificialntelligence_presentation.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
Assigned Numbers - 2025 - Bluetooth® Document
Spectroscopy.pptx food analysis technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Reach Out and Touch Someone: Haptics and Empathic Computing
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
A comparative analysis of optical character recognition models for extracting...
NewMind AI Weekly Chronicles - August'25-Week II
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”

Java script good_bad_awful

  • 1. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References JavaScript The good, bad and awful parts Moamen Mokhtar Usama Elnily September 17, 2013 Moamen Mokhtar, Usama Elnily JavaScript
  • 2. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 3. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Brief History Facts about JS Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 4. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Brief History Facts about JS What is Javascript The world most missunderstood programming language (Neither Java nor Script!) Lots of design errors. Moamen Mokhtar, Usama Elnily JavaScript
  • 5. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Brief History Facts about JS History In 1992, James Gosling and java. Netscape (Berndan Eich) and LiveScript. Sun, Netscape and JavaScript. Moamen Mokhtar, Usama Elnily JavaScript
  • 6. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Brief History Facts about JS Javascript the language Load and go delivery. Loose typing. Objects as general containers. Prototypal inheritance. Lambda. Linkage through global variables. Moamen Mokhtar, Usama Elnily JavaScript
  • 7. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Brief History Facts about JS Facts about JS NaN NaN === NaN == ’1’ == 1 ” == false typeof typeof ”help”.toString; for .. in obj. obj[’ ’] Moamen Mokhtar, Usama Elnily JavaScript
  • 8. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 9. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Function definition var foo = function(); function foo (); Moamen Mokhtar, Usama Elnily JavaScript
  • 10. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Augmenting Function.prototype.method = function(name , func) { this.prototype[name] = func; return this; }; Moamen Mokhtar, Usama Elnily JavaScript
  • 11. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Scope I No block Scope. Hoisting. foo (); // not visible bar (); // visible var foo = function (){ ... }; function bar (){ ... }; Moamen Mokhtar, Usama Elnily JavaScript
  • 12. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Scope II function foo (){ function bar() { return 3; } return bar (); function bar() { return 8; } } alert(foo ()); Moamen Mokhtar, Usama Elnily JavaScript
  • 13. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Scope III function foo (){ var bar = function () { return 3; }; return bar (); var bar = function () { return 8; }; } alert(foo ()); Moamen Mokhtar, Usama Elnily JavaScript
  • 14. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Scope IV alert(foo ()); function foo (){ var bar = function () { return 3; }; return bar (); var bar = function () { return 8; }; } Moamen Mokhtar, Usama Elnily JavaScript
  • 15. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Scope V function foo (){ return bar (); var bar = function () { return 3; }; var bar = function () { return 8; }; } alert(foo ()); Moamen Mokhtar, Usama Elnily JavaScript
  • 16. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Scope VI var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar (); Moamen Mokhtar, Usama Elnily JavaScript
  • 17. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Arguments An array-like variable. Contains all of the arguments of the functions. Doesn’t have all the functions of arrays except length(). Moamen Mokhtar, Usama Elnily JavaScript
  • 18. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Apply and Call function invocation Difference between apply and call. theFunction.apply(valueForThis , arrayOfArgs) theFunction.call(valueForThis , arg1 , arg2 , ...) Moamen Mokhtar, Usama Elnily JavaScript
  • 19. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Closure var myObject = function ( ) { var value = 0; return { increment: function (inc) { value += typeof inc === ’number ’ ? inc : 1; }, getValue: function ( ) { return value; } } }( ); Moamen Mokhtar, Usama Elnily JavaScript
  • 20. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Curry Function.method(’curry ’, function ( ) { var slice = Array.prototype.slice , args = slice.apply(arguments), that = this; return function ( ) { return that.apply(null , args.concat(slice.apply(arguments ))); }; }); Creates closure to hold arguments of the outer function to be used in the inner function. Moamen Mokhtar, Usama Elnily JavaScript
  • 21. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Memoization I var memoizer = function (memo , fundamental) { var shell = function (n) { var result = memo[n]; if (typeof result !== ’number ’) { result = fundamental(shell , n); memo[n] = result; } return result; }; return shell; }; Moamen Mokhtar, Usama Elnily JavaScript
  • 22. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Function definition Augmenting Scope Arguments Apply vs. Call Closure Curry Memoization Memoization II var fibonacci = memoizer ([0, 1], function (shell , n) { return shell(n - 1) + shell(n - 2); }); var factorial = memoizer ([1, 1], function (shell , n) { return n * shell(n - 1); }); Moamen Mokhtar, Usama Elnily JavaScript
  • 23. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Important methods Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 24. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Important methods Important methods array.slice(start, end ) It makes a copy of a portion of the array starting from start to end. array.splice(start, deleteCount, item...) The splice method removes elements from an array, replacing them with new items. It is different than delete as delete will replace the deleted value with undefined array.sort(comparefn ) The default sort function converts the array entries into strings and sort lexicographically!. It is provided with the sorting function (like comparable objects in java). Moamen Mokhtar, Usama Elnily JavaScript
  • 25. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 26. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks Using async. functions Prefered with discontinous events to prevent frozen clients. What is a callback? Moamen Mokhtar, Usama Elnily JavaScript
  • 27. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks for(vari =1;i <=3;i++){ setTimeout(function (){ console.log(i);} ,0); }; Moamen Mokhtar, Usama Elnily JavaScript
  • 28. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks The devil is in the details Javascript is single threaded. Meet the queue. Moamen Mokhtar, Usama Elnily JavaScript
  • 29. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks Sometimes-async functions I Functions that are async sometimes, but not at other times. Example: Async functions with caching. var socketsCache ={}; function openWebSocket(serverAddress ,callback ){ var socket; if(serverAddress in webSocketCache ){ socket=webSocketCache [serverAddress ]; if(socket.readyState === WebSocket.OPEN ){ callback (); } .... Moamen Mokhtar, Usama Elnily JavaScript
  • 30. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks Sometimes-async functions II }else{ socket=newWebSocket(serverAddress ); webSocketCache [serverAddress ]= socket; .... }; return socket; }; var socket=openWebSocket(url ,function (){ .... }); .... Moamen Mokhtar, Usama Elnily JavaScript
  • 31. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Why using async. functions Sometimes-async functions Throwing errors from callbacks Throwing errors from callbacks Can we catch error thrown from a callabck ? try{ setTimeout(function (){ throw new Error(’Catch_me_if_you_can !’); },0); }catch(e){ console.error(e); } Moamen Mokhtar, Usama Elnily JavaScript
  • 32. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 33. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Awful and bad parts I global variables A non-declared variable is considered global. Semicolon insertion return { status: true }; Moamen Mokhtar, Usama Elnily JavaScript
  • 34. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References Awful and bad parts II Reserved Words Use them within quotes. Access with subscript object = {case: value }; // illegal object = {’case ’: value }; // ok object.case = value; // illegal object[’case ’] = value; // ok Bitwise Operators Very slow! Moamen Mokhtar, Usama Elnily JavaScript
  • 35. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References References Outline 1 Intro 2 Functions 3 Arrays 4 Asyncrounus Javascript 5 Awful and bad parts 6 References Moamen Mokhtar, Usama Elnily JavaScript
  • 36. Intro Functions Arrays Asyncrounus Javascript Awful and bad parts References References References Douglas Crockford. JavaScript: The Good Parts. O’Reilly, 2008. Trevor Burnham. Async JavaScript. The Pragmatic Programmers, 2012. Moamen Mokhtar, Usama Elnily JavaScript