36
/** * 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 dont tell you about Raphael Cruzeiro http://raphaelcruzeiro.com @rcdeveloper Wednesday, July 10, 13

What they don't tell you about JavaScript

Embed Size (px)

Citation preview

Page 1: What they don't tell you about JavaScript

/** * 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 ); };

JavaScriptWhat they don’t tell you about

Raphael Cruzeirohttp://raphaelcruzeiro.com@rcdeveloper

Wednesday, July 10, 13

Page 2: What they don't tell you about JavaScript

What does JavaScript have in common with Java?

Wednesday, July 10, 13

Page 3: What they don't tell you about JavaScript

What does JavaScript have in common with Java?

The first four letters of its name.

Wednesday, July 10, 13

Page 4: What they don't tell you about JavaScript

It actually

has a lot more in common with

Python

and other dynamic and functional languages

Wednesday, July 10, 13

Page 5: What they don't tell you about JavaScript

Originally named

LiveScript

Wednesday, July 10, 13

Page 6: What they don't tell you about JavaScript

It was created by Netscape to enhance web pages.

Wednesday, July 10, 13

Page 7: What they don't tell you about JavaScript

It is used today not only in the browser but also to

create desktop widgets and server-side code.

Wednesday, July 10, 13

Page 8: What they don't tell you about JavaScript

Formalized in ECMAScript.

Wednesday, July 10, 13

Page 9: What they don't tell you about JavaScript

“The problem with JavaScript is that nobody tries to learn it before writing applications

with it.” - Douglas Crockford

Wednesday, July 10, 13

Page 10: What they don't tell you about JavaScript

JavaScript

makes it

easy to write terrible code.

Wednesday, July 10, 13

Page 11: What they don't tell you about JavaScript

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

Page 12: What they don't tell you about JavaScript

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

Page 13: What they don't tell you about JavaScript

Global scope polluting

Wednesday, July 10, 13

Page 14: What they don't tell you about JavaScript

var something = 4;

Wednesday, July 10, 13

Page 15: What they don't tell you about JavaScript

When declaring variables on the global scope you make

your code more susceptible to name clashes.

Wednesday, July 10, 13

Page 16: What they don't tell you about JavaScript

In JavaScript a function delimits the scope of

variables

Wednesday, July 10, 13

Page 17: What they don't tell you about JavaScript

var something = 4;

function foo () { somethingElse = something + 4; return somethingElse;};

foo();

Accidental use of global variables

Wednesday, July 10, 13

Page 18: What they don't tell you about JavaScript

Dangers of

Hoisting

Wednesday, July 10, 13

Page 19: What they don't tell you about JavaScript

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

Page 20: What they don't tell you about JavaScript

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

Page 21: What they don't tell you about JavaScript

Objects

with no

classesWednesday, July 10, 13

Page 22: What they don't tell you about JavaScript

Pretty much

everything

is an object in

JavaScript

Wednesday, July 10, 13

Page 23: What they don't tell you about JavaScript

With a few exceptions:

•Number

•String

•Boolean

•null

•undefined

Wednesday, July 10, 13

Page 24: What they don't tell you about JavaScript

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.20.30000000000000004

Wednesday, July 10, 13

Page 25: What they don't tell you about JavaScript

Creating an object:

var obj = new Object();// Or via the object literalvar obj = {};

Wednesday, July 10, 13

Page 26: What they don't tell you about JavaScript

Adding attributes:

var obj = {};

obj.a = 15;obj.bark = function() { return “Woof!”;};

Wednesday, July 10, 13

Page 27: What they don't tell you about JavaScript

Adding attributes:

var obj = { a: 15, bark: function() { return “Woof!”; }};

Wednesday, July 10, 13

Page 28: What they don't tell you about JavaScript

Constructors:

function Point(x, y) { this.x = x; this.y = y;}

var point = new Point(60, 578);

Wednesday, July 10, 13

Page 29: What they don't tell you about JavaScript

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

Page 30: What they don't tell you about JavaScript

Ensuring

that the constructor can work with

or without

newWednesday, July 10, 13

Page 31: What they don't tell you about JavaScript

Constructors:

function Point(x, y) { if (!(this instanceof Point)) { return new Point(x, y); } this.x = x; this.y = y;}

Wednesday, July 10, 13

Page 32: What they don't tell you about JavaScript

Modularizingcode withclosures

Wednesday, July 10, 13

Page 33: What they don't tell you about JavaScript

(function() {

/* Everything that is declared here is local to this closure only */

})();

Wednesday, July 10, 13

Page 34: What they don't tell you about JavaScript

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

Page 35: What they don't tell you about JavaScript

You can use closures to emulate namespaces:

var pt = new space.Point(60, 568);

Wednesday, July 10, 13

Page 36: What they don't tell you about JavaScript

That’s itat least for now...

Wednesday, July 10, 13