What they don't tell you about JavaScript

Preview:

Citation preview

/** * 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

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

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

Wednesday, July 10, 13

Creating an object:

var obj = new Object();// Or via the object literalvar 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

Modularizingcode withclosures

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 itat least for now...

Wednesday, July 10, 13