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

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 

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