1 / 26

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

I just met you, and 'this' is crazy, but here's my NaN, so call(me) maybe?. JavaScript is so weird. 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 E quality

aiko-tran
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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!

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

  4. JavaScript ...is not Java Java is to JavaScript as car is to carpet Java is to JavaScript as ham is to hamster

  5. Syntax Error • Automatic semicolon silliness • {} • ;

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

  7. Just say no to single line blocks In reality it is (to the parser) if (ok) { x = true; } callFunc(); Devs turn into if (ok) x = true; callFunc(); Then looks like (to the dev) if (ok) x = true; callFunc(); One single line if (ok) x = true;

  8. Putting the "fun" in functions function functionStatement() { // Some code return value; } var functionExpression = function() { // Some code return value; };

  9. Putting the "fun" in functions (function () { console.log("anonymous function"); })(); (function IIFE() { console.log("function expression"); })();

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

  11. DEMO • Fun with functions

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

  13. Carry on • continue statement "I've never seen a piece of code that was not improved by removing it" -- Crockford

  14. The switch switch (expression) { case expression: statements [break;] case expression: statements [break;] default: statements [break;] } • Auto fallthrough

  15. Let's get to the truth of the matter Truthy values 'false' (quoted false) '0' (quoted zero) () (empty functions) [] (empty arrays) {} (empty objects) All other values Falsey values false 0 (zero) '' (empty string) null undefined NaN

  16. All things being equal-ish. Maybe. == === != !==

  17. DEMO • Truthy and Falsey

  18. Just what's up with this, anyway?

  19. Eval - the most evil of all? Or just a lowly, misunderstood, function? var x = eval("forms[0].option" + optionNumber + ".checked"); var x = forms[0]["option" + optionNumber].checked;

  20. Are you with me, or against me? with (objX) { // statements }

  21. New, new, don't do • typed wrappers • new object • new array • new Date

  22. farceInt(fib); static intparseInt(String s) static intparseInt(String s, int radix)

  23. parseInt's farce parsing 0X or 0x 16 (hexadecimal) var result = parseInt(numericString, 10); 0 8 (octal) 10 (decimal) Everything else 10 (decimal)

  24. 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()

  25. Seems legit Is legit var add = function(where are the arguments?) { return arguments[0] + arguments[1]; }; console.log(add(4, 4)); // returns 8 var add = function() { return arguments[0] + arguments[1]; }; console.log(add(4, 4)); // returns 8

  26. How to avoid all the odd stuff • JSLint • JSHint • JSFiddle

More Related