i just met you, and "this" is crazy, but here's my nan, so call(me), maybe? by rachel...

26
I just met you, and 'this' is crazy, but here's my NaN, so call(me) maybe? JavaScript is so weird

Upload: net-conf-uy

Post on 19-Jun-2015

108 views

Category:

Technology


3 download

DESCRIPTION

I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? Rachel Appel .NET Conf UY 2014 http://netconf.uy

TRANSCRIPT

Page 1: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

I just met you, and 'this' is crazy, but here's my

NaN, so call(me) maybe?JavaScript is so weird

Page 2: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

What makes JavaScript fun?(and by fun I mean....a little bit strange)

• Eval• parseInt• NaN• With• JSLint/JSHint• Arrays• Switches• more...

• JavaScript • Blocks• Functions • Null• Equality• Truthy/Falsey• Objects• this!

Page 3: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

http://i.imgur.com/wR3ZxfB.jpg

Page 4: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

JavaScript...is not Java

Java is to JavaScript as car is to carpet

Java is to JavaScript as ham is to hamster

Page 5: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Syntax Error

• Automatic semicolon silliness• {}• ;

Page 6: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Syntax Error

• Most often, a newline (\n) ends a statement unless...• The statement has an unclosed parenthesis ")", array literal, or object literal.• The line uses -- or ++• Any block statements such as for, while, do, if, or else, and there is no starting

bracket "{" • After these constructs: break, continue, return, throw

Page 7: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Just say no to single line blocks

One single line

if (ok) x = true;

Then looks like(to the dev)

if (ok) x = true; callFunc();

Devs turn into

if (ok) x = true; callFunc();

In reality it is(to the parser)

if (ok) {x = true; }callFunc();

Page 8: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Putting the "fun" in functions

function functionStatement() { // Some code return value;}var functionExpression = function() { // Some code return value;};

Page 9: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Putting the "fun" in functions

(function IIFE() { console.log("function expression");})();

(function () { console.log("anonymous function");})();

Page 10: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Putting the "fun" in functionsfunction main() { functionStatement(); function functionStatement() { ... } // oh noes! this won't work functionExpression(); var functionExpression = function() { ... } }

Page 11: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

DEMO

• Fun with functions

Page 12: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Arrays

• There is no such thing• No dimensions• No out of bounds errors• typeof doesn't know the difference

Page 13: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Carry on

• continue statement

"I've never seen a piece of code that was not improved by removing it" -- Crockford

Page 14: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

The switch• Auto fallthrough switch (expression) {

case expression: statements [break;] case expression: statements [break;] default: statements [break;] }

Page 15: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Truthy values

'false' (quoted false)'0' (quoted zero)() (empty functions)[] (empty arrays){} (empty objects)All other values

Falsey values

false 0 (zero) ''  (empty string)nullundefinedNaN

Let's get to the truth of the matter

Page 16: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

== ===

All things being equal-ish. Maybe.

!= !==

Page 17: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

DEMO

• Truthy and Falsey

Page 18: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Just what's up with this, anyway?

Page 19: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Or just a lowly, misunderstood, function?

Eval - the most evil of all?

var x = eval("forms[0].option" + optionNumber + ".checked");

var x = forms[0]["option" + optionNumber].checked;

Page 20: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Are you with me, or against me?

with (objX) { // statements}

Page 21: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

New, new, don't do

• typed wrappers• new object• new array• new Date

Page 22: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

farceInt(fib);

static int parseInt(String s)

static int parseInt(String s, int radix)

Page 23: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

parseInt's farce parsing

var result = parseInt(numericString, 10);

0X or 0x 16 (hexadecimal)

0 10 (decimal)

Everything else 10 (decimal)

8 (octal)

Page 24: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

NaN

• It claims it's NotANumber, but it's a number. • Don't use equality operators with NaN • Use Number.isNaN() instead• Use typeof instead• Don't use plain isNaN() – or else nothing is a number!

• ES6 Number.isNaN()

Page 25: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

Seems legit

var add = function() { return arguments[0] + arguments[1];};

console.log(add(4, 4)); // returns 8

Is legit

var add = function(where are the arguments?) { return arguments[0] + arguments[1];};

console.log(add(4, 4)); // returns 8

Page 26: I just met you, and "this" is crazy, but here's my NaN, so call(me), maybe? by Rachel Appel

How to avoid all the odd stuff

• JSLint• JSHint• JSFiddle