SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
/**
* southWest
* Gets the southwestern quadrant of the tree
* return {Quadtree}
**/
Quadtree.prototype.southWest = function() {
return new Quadtree(
new space2d.Point(
this._origin.x,
Math.floor(this._size.y / 2)
),
new space2d.Point(
Math.floor(this._size.x / 2),
this._size.y
),
this
);
};
/**
* southEast
* Gets the southeastern quadrant of the tree
* return {Quadtree}
**/
Quadtree.prototype.southEast = function() {
return new Quadtree(
new space2d.Point(
Math.floor(this._size.x / 2),
Math.floor(this._size.y / 2)
),
new space2d.Point(
this._size.x,
this._size.y
),
this
);
};
JavaScript
What they don’t tell you about
Raphael Cruzeiro
http://raphaelcruzeiro.com
@rcdeveloper
Wednesday, July 10, 13
What does JavaScript have in
common with Java?
Wednesday, July 10, 13
What does JavaScript have in
common with Java?
The first four letters of its name.
Wednesday, July 10, 13
It actually
has a lot more in common with
Python
and other dynamic and
functional languages
Wednesday, July 10, 13
Originally named
LiveScript
Wednesday, July 10, 13
It was created by Netscape to
enhance web pages.
Wednesday, July 10, 13
It is used today not only in
the browser but also to
create desktop widgets and
server-side code.
Wednesday, July 10, 13
Formalized in ECMAScript.
Wednesday, July 10, 13
“The problem with JavaScript is that nobody
tries to learn it before writing applications
with it.” - Douglas Crockford
Wednesday, July 10, 13
JavaScript
makes it
easy
to write
terrible code.
Wednesday, July 10, 13
var currentId;
var lastViewPort;
var precision = Math.pow(10, 4);
var maxPointNumber = 100;
var circleOverlays = [];
function didViewPortChange() {
var minLat = lastViewPort.getSouthWest().lat() -
(lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat());
var minLng = lastViewPort.getSouthWest().lng() -
(lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng());
var maxLat = lastViewPort.getNorthEast().lat() +
(lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat());
var maxLng = lastViewPort.getNorthEast().lng() +
(lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng());
if ($.fn.jqMap.getMap().getBounds().getNorthEast().lat() > maxLat ||
$.fn.jqMap.getMap().getBounds().getNorthEast().lng() > maxLng ||
$.fn.jqMap.getMap().getBounds().getSouthWest().lat() < minLat ||
$.fn.jqMap.getMap().getBounds().getSouthWest().lng() < minLng) {
return true;
}
else {
return false;
}
}
Really bad code:
Wednesday, July 10, 13
var currentId;
var lastViewPort;
var precision = Math.pow(10, 4);
var maxPointNumber = 100;
var circleOverlays = [];
function didViewPortChange() {
var minLat = lastViewPort.getSouthWest().lat() -
(lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat());
var minLng = lastViewPort.getSouthWest().lng() -
(lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng());
var maxLat = lastViewPort.getNorthEast().lat() +
(lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat());
var maxLng = lastViewPort.getNorthEast().lng() +
(lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng());
if ($.fn.jqMap.getMap().getBounds().getNorthEast().lat() > maxLat ||
$.fn.jqMap.getMap().getBounds().getNorthEast().lng() > maxLng ||
$.fn.jqMap.getMap().getBounds().getSouthWest().lat() < minLat ||
$.fn.jqMap.getMap().getBounds().getSouthWest().lng() < minLng) {
return true;
}
else {
return false;
}
}
Really bad code:
GUILTY
Wednesday, July 10, 13
Global scope polluting
Wednesday, July 10, 13
var something = 4;
Wednesday, July 10, 13
When declaring variables on
the global scope you make
your code more susceptible to
name clashes.
Wednesday, July 10, 13
In JavaScript a function
delimits the scope of
variables
Wednesday, July 10, 13
var something = 4;
function foo () {
somethingElse = something + 4;
return somethingElse;
};
foo();
Accidental use of global
variables
Wednesday, July 10, 13
Dangers of
Hoisting
Wednesday, July 10, 13
var something = 4;
var somethingElse = 7;
function foo () {
console.log(something);
console.log(somethingElse);
var something = “This is confusing.”;
console.log(something);
};
foo();
Wednesday, July 10, 13
var something = 4;
var somethingElse = 7;
function foo () {
console.log(something);
console.log(somethingElse);
var something = “This is confusing.”;
console.log(something);
};
foo();
Outputs:
undefined
7
This is confusing.
Wednesday, July 10, 13
Objects
with no
classes
Wednesday, July 10, 13
Pretty much
everything
is an object in
JavaScript
Wednesday, July 10, 13
With a few exceptions:
•Number
•String
•Boolean
•null
•undefined
Wednesday, July 10, 13
A side-note about Numbers:
•There is only one type for representing all
numbers.
•It is represented in memory as a 64-bit
floating point
•IEEE-754
> 0.1 + 0.2
0.30000000000000004
Wednesday, July 10, 13
Creating an object:
var obj = new Object();
// Or via the object literal
var obj = {};
Wednesday, July 10, 13
Adding attributes:
var obj = {};
obj.a = 15;
obj.bark = function() {
return “Woof!”;
};
Wednesday, July 10, 13
Adding attributes:
var obj = {
a: 15,
bark: function() {
return “Woof!”;
}
};
Wednesday, July 10, 13
Constructors:
function Point(x, y) {
this.x = x;
this.y = y;
}
var point = new Point(60, 578);
Wednesday, July 10, 13
If you forget the new:
var point = Point(60, 578);
point == undefined
x and y are now variables on the
global scope
Wednesday, July 10, 13
Ensuring
that the constructor
can work with
or without
newWednesday, July 10, 13
Constructors:
function Point(x, y) {
if (!(this instanceof Point)) {
return new Point(x, y);
}
this.x = x;
this.y = y;
}
Wednesday, July 10, 13
Modularizing
code with
closures
Wednesday, July 10, 13
(function() {
/* Everything that is declared
here is local to this closure only */
})();
Wednesday, July 10, 13
You can use closures to emulate
namespaces:
var space = (function(){
var Point = function(x, y) {
if (!(this instanceof Point)) {
return new Point(x, y);
}
this.x = x;
this.y = y;
};
return {
Point: Point
};
})();
Wednesday, July 10, 13
You can use closures to emulate
namespaces:
var pt = new space.Point(60, 568);
Wednesday, July 10, 13
That’s it
at least for now...
Wednesday, July 10, 13

Weitere ähnliche Inhalte

Was ist angesagt?

Bindings: the zen of montage
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montage
Kris Kowal
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
Sages
 
Ogdc 2013 lets remake the wheel
Ogdc 2013 lets remake the wheelOgdc 2013 lets remake the wheel
Ogdc 2013 lets remake the wheel
Son Aris
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
Yasuharu Nakano
 

Was ist angesagt? (20)

Python queue solution with asyncio and kafka
Python queue solution with asyncio and kafkaPython queue solution with asyncio and kafka
Python queue solution with asyncio and kafka
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
MiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScriptMiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScript
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196
 
W3C HTML5 KIG-How to write low garbage real-time javascript
W3C HTML5 KIG-How to write low garbage real-time javascriptW3C HTML5 KIG-How to write low garbage real-time javascript
W3C HTML5 KIG-How to write low garbage real-time javascript
 
Bindings: the zen of montage
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montage
 
Zone.js 2017
Zone.js 2017Zone.js 2017
Zone.js 2017
 
Développer avec un Simple Object Mapping Toolkit pour SQL Server
Développer avec un Simple Object Mapping Toolkit pour SQL ServerDévelopper avec un Simple Object Mapping Toolkit pour SQL Server
Développer avec un Simple Object Mapping Toolkit pour SQL Server
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
 
Ogdc 2013 lets remake the wheel
Ogdc 2013 lets remake the wheelOgdc 2013 lets remake the wheel
Ogdc 2013 lets remake the wheel
 
OGDC2013_Lets remake the wheel_ Mr Nguyen Trung Hung
OGDC2013_Lets remake the wheel_ Mr Nguyen Trung HungOGDC2013_Lets remake the wheel_ Mr Nguyen Trung Hung
OGDC2013_Lets remake the wheel_ Mr Nguyen Trung Hung
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
Debugging JavaScript with Chrome
Debugging JavaScript with ChromeDebugging JavaScript with Chrome
Debugging JavaScript with Chrome
 
Moar tools for asynchrony!
Moar tools for asynchrony!Moar tools for asynchrony!
Moar tools for asynchrony!
 
The Ring programming language version 1.9 book - Part 63 of 210
The Ring programming language version 1.9 book - Part 63 of 210The Ring programming language version 1.9 book - Part 63 of 210
The Ring programming language version 1.9 book - Part 63 of 210
 
You will learn RxJS in 2017
You will learn RxJS in 2017You will learn RxJS in 2017
You will learn RxJS in 2017
 
Call stack, event loop and async programming
Call stack, event loop and async programmingCall stack, event loop and async programming
Call stack, event loop and async programming
 
Fixing Web Data in Production
Fixing Web Data in ProductionFixing Web Data in Production
Fixing Web Data in Production
 
The State of JavaScript
The State of JavaScriptThe State of JavaScript
The State of JavaScript
 
Shift Remote FRONTEND: Reactivity in Vue.JS 3 - Marko Boskovic (Barrage)
Shift Remote FRONTEND: Reactivity in Vue.JS 3 - Marko Boskovic (Barrage)Shift Remote FRONTEND: Reactivity in Vue.JS 3 - Marko Boskovic (Barrage)
Shift Remote FRONTEND: Reactivity in Vue.JS 3 - Marko Boskovic (Barrage)
 

Andere mochten auch

China presentation
China presentationChina presentation
China presentation
Alexander_zl
 
OMEGA+
OMEGA+OMEGA+
OMEGA+
dpmtl
 
Mul-t-Lock Matrix
Mul-t-Lock MatrixMul-t-Lock Matrix
Mul-t-Lock Matrix
dpmtl
 
Excellent school performance at age 16 and risk of adult bipolar disorder na...
Excellent school performance at age 16 and risk of adult bipolar disorder  na...Excellent school performance at age 16 and risk of adult bipolar disorder  na...
Excellent school performance at age 16 and risk of adult bipolar disorder na...
Alexander_zl
 

Andere mochten auch (13)

China presentation
China presentationChina presentation
China presentation
 
Michael phelps
Michael phelpsMichael phelps
Michael phelps
 
Michael phelps (bloc)
Michael phelps (bloc)Michael phelps (bloc)
Michael phelps (bloc)
 
India uind[1]
India uind[1]India uind[1]
India uind[1]
 
خزانات مياه المصريه الفسطينيه للتوريدات
خزانات مياه المصريه الفسطينيه للتوريداتخزانات مياه المصريه الفسطينيه للتوريدات
خزانات مياه المصريه الفسطينيه للتوريدات
 
OMEGA+
OMEGA+OMEGA+
OMEGA+
 
Mul-t-Lock Matrix
Mul-t-Lock MatrixMul-t-Lock Matrix
Mul-t-Lock Matrix
 
Exploratory study of the approach to school leadership development programmes...
Exploratory study of the approach to school leadership development programmes...Exploratory study of the approach to school leadership development programmes...
Exploratory study of the approach to school leadership development programmes...
 
Power point.
Power point.Power point.
Power point.
 
Excellent school performance at age 16 and risk of adult bipolar disorder na...
Excellent school performance at age 16 and risk of adult bipolar disorder  na...Excellent school performance at age 16 and risk of adult bipolar disorder  na...
Excellent school performance at age 16 and risk of adult bipolar disorder na...
 
China quiz
China quizChina quiz
China quiz
 
Los medios de cohesión textual
Los medios de cohesión textualLos medios de cohesión textual
Los medios de cohesión textual
 
Estructura textual
Estructura textualEstructura textual
Estructura textual
 

Ähnlich wie What they don't tell you about JavaScript

Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul
 
ECMAScript 6 major changes
ECMAScript 6 major changesECMAScript 6 major changes
ECMAScript 6 major changes
hayato
 

Ähnlich wie What they don't tell you about JavaScript (20)

ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
Javascript
JavascriptJavascript
Javascript
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
Myraytracer
MyraytracerMyraytracer
Myraytracer
 
ECMAScript 6 major changes
ECMAScript 6 major changesECMAScript 6 major changes
ECMAScript 6 major changes
 
Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Watch out: Observables are here to stay
Watch out: Observables are here to stayWatch out: Observables are here to stay
Watch out: Observables are here to stay
 
Groovy
GroovyGroovy
Groovy
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
Code level change propagation in virtual platform
Code level change propagation in virtual platformCode level change propagation in virtual platform
Code level change propagation in virtual platform
 
Developing High Performance Websites and Modern Apps with JavaScript and HTML5
Developing High Performance Websites and Modern Apps with JavaScript and HTML5Developing High Performance Websites and Modern Apps with JavaScript and HTML5
Developing High Performance Websites and Modern Apps with JavaScript and HTML5
 
8558537werr.pptx
8558537werr.pptx8558537werr.pptx
8558537werr.pptx
 

Kürzlich hochgeladen

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 

Kürzlich hochgeladen (20)

Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Your enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jYour enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4j
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 

What they don't tell you about JavaScript

  • 1. /** * southWest * Gets the southwestern quadrant of the tree * return {Quadtree} **/ Quadtree.prototype.southWest = function() { return new Quadtree( new space2d.Point( this._origin.x, Math.floor(this._size.y / 2) ), new space2d.Point( Math.floor(this._size.x / 2), this._size.y ), this ); }; /** * southEast * Gets the southeastern quadrant of the tree * return {Quadtree} **/ Quadtree.prototype.southEast = function() { return new Quadtree( new space2d.Point( Math.floor(this._size.x / 2), Math.floor(this._size.y / 2) ), new space2d.Point( this._size.x, this._size.y ), this ); }; JavaScript What they don’t tell you about Raphael Cruzeiro http://raphaelcruzeiro.com @rcdeveloper Wednesday, July 10, 13
  • 2. What does JavaScript have in common with Java? Wednesday, July 10, 13
  • 3. What does JavaScript have in common with Java? The first four letters of its name. Wednesday, July 10, 13
  • 4. It actually has a lot more in common with Python and other dynamic and functional languages Wednesday, July 10, 13
  • 6. It was created by Netscape to enhance web pages. Wednesday, July 10, 13
  • 7. It is used today not only in the browser but also to create desktop widgets and server-side code. Wednesday, July 10, 13
  • 9. “The problem with JavaScript is that nobody tries to learn it before writing applications with it.” - Douglas Crockford Wednesday, July 10, 13
  • 10. JavaScript makes it easy to write terrible code. Wednesday, July 10, 13
  • 11. var currentId; var lastViewPort; var precision = Math.pow(10, 4); var maxPointNumber = 100; var circleOverlays = []; function didViewPortChange() { var minLat = lastViewPort.getSouthWest().lat() - (lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat()); var minLng = lastViewPort.getSouthWest().lng() - (lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng()); var maxLat = lastViewPort.getNorthEast().lat() + (lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat()); var maxLng = lastViewPort.getNorthEast().lng() + (lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng()); if ($.fn.jqMap.getMap().getBounds().getNorthEast().lat() > maxLat || $.fn.jqMap.getMap().getBounds().getNorthEast().lng() > maxLng || $.fn.jqMap.getMap().getBounds().getSouthWest().lat() < minLat || $.fn.jqMap.getMap().getBounds().getSouthWest().lng() < minLng) { return true; } else { return false; } } Really bad code: Wednesday, July 10, 13
  • 12. var currentId; var lastViewPort; var precision = Math.pow(10, 4); var maxPointNumber = 100; var circleOverlays = []; function didViewPortChange() { var minLat = lastViewPort.getSouthWest().lat() - (lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat()); var minLng = lastViewPort.getSouthWest().lng() - (lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng()); var maxLat = lastViewPort.getNorthEast().lat() + (lastViewPort.getNorthEast().lat() - lastViewPort.getSouthWest().lat()); var maxLng = lastViewPort.getNorthEast().lng() + (lastViewPort.getNorthEast().lng() - lastViewPort.getSouthWest().lng()); if ($.fn.jqMap.getMap().getBounds().getNorthEast().lat() > maxLat || $.fn.jqMap.getMap().getBounds().getNorthEast().lng() > maxLng || $.fn.jqMap.getMap().getBounds().getSouthWest().lat() < minLat || $.fn.jqMap.getMap().getBounds().getSouthWest().lng() < minLng) { return true; } else { return false; } } Really bad code: GUILTY Wednesday, July 10, 13
  • 14. var something = 4; Wednesday, July 10, 13
  • 15. When declaring variables on the global scope you make your code more susceptible to name clashes. Wednesday, July 10, 13
  • 16. In JavaScript a function delimits the scope of variables Wednesday, July 10, 13
  • 17. var something = 4; function foo () { somethingElse = something + 4; return somethingElse; }; foo(); Accidental use of global variables Wednesday, July 10, 13
  • 19. var something = 4; var somethingElse = 7; function foo () { console.log(something); console.log(somethingElse); var something = “This is confusing.”; console.log(something); }; foo(); Wednesday, July 10, 13
  • 20. var something = 4; var somethingElse = 7; function foo () { console.log(something); console.log(somethingElse); var something = “This is confusing.”; console.log(something); }; foo(); Outputs: undefined 7 This is confusing. Wednesday, July 10, 13
  • 22. Pretty much everything is an object in JavaScript Wednesday, July 10, 13
  • 23. With a few exceptions: •Number •String •Boolean •null •undefined Wednesday, July 10, 13
  • 24. A side-note about Numbers: •There is only one type for representing all numbers. •It is represented in memory as a 64-bit floating point •IEEE-754 > 0.1 + 0.2 0.30000000000000004 Wednesday, July 10, 13
  • 25. Creating an object: var obj = new Object(); // Or via the object literal var obj = {}; Wednesday, July 10, 13
  • 26. Adding attributes: var obj = {}; obj.a = 15; obj.bark = function() { return “Woof!”; }; Wednesday, July 10, 13
  • 27. Adding attributes: var obj = { a: 15, bark: function() { return “Woof!”; } }; Wednesday, July 10, 13
  • 28. Constructors: function Point(x, y) { this.x = x; this.y = y; } var point = new Point(60, 578); Wednesday, July 10, 13
  • 29. If you forget the new: var point = Point(60, 578); point == undefined x and y are now variables on the global scope Wednesday, July 10, 13
  • 30. Ensuring that the constructor can work with or without newWednesday, July 10, 13
  • 31. Constructors: function Point(x, y) { if (!(this instanceof Point)) { return new Point(x, y); } this.x = x; this.y = y; } Wednesday, July 10, 13
  • 33. (function() { /* Everything that is declared here is local to this closure only */ })(); Wednesday, July 10, 13
  • 34. You can use closures to emulate namespaces: var space = (function(){ var Point = function(x, y) { if (!(this instanceof Point)) { return new Point(x, y); } this.x = x; this.y = y; }; return { Point: Point }; })(); Wednesday, July 10, 13
  • 35. You can use closures to emulate namespaces: var pt = new space.Point(60, 568); Wednesday, July 10, 13
  • 36. That’s it at least for now... Wednesday, July 10, 13