51
So you think you know JavaScript? 1 Matt Apperson Founder / Owner

So you think you know JavaScript

Embed Size (px)

Citation preview

Page 1: So you think you know JavaScript

So you think you know JavaScript?

1

Matt AppersonFounder / Owner

Page 2: So you think you know JavaScript

© 2013 Apperson Labs

Matt [email protected]

http://appersonlabs.com

twitter: @appersonlabs

Page 3: So you think you know JavaScript

3

© 2013 Apperson Labs

JavaScript is a great language!

Page 4: So you think you know JavaScript

© 2013 Apperson Labs

But do a quick Google search and you see that many have no clue how to use it correctly to build apps

WHY?

Page 5: So you think you know JavaScript

© 2013 Apperson Labs

Ask any developer who has ever touched a website, and he will say he knows JavaScript...

WRONG?

Page 6: So you think you know JavaScript

© 2013 Apperson Labs

Seen this page much?

Page 7: So you think you know JavaScript

© 2013 Apperson Labs

Go here!

http://goo.gl/cQgzw

Page 8: So you think you know JavaScript

© 2013 Apperson Labs

DEATH TO CoffeeScript!!And all frameworks...

Page 9: So you think you know JavaScript

9

© 2013 Apperson Labs

Page 10: So you think you know JavaScript

10

© 2013 Apperson Labs

• JS has been around for a while, but it has been the red headed step child

Page 11: So you think you know JavaScript

11

© 2013 Apperson Labs

• JS has been around for a while, but it has been the red headed step child• Most JS examples and tutorials are legacy, hacks, and based on use in web pages

Page 12: So you think you know JavaScript

12

© 2013 Apperson Labs

• JS has been around for a while, but it has been the red headed step child• Most JS examples and tutorials are legacy, hacks, and based on use in web pages• JS is NOT strongly typed, so it’s easy to turn it into spaghetti code

Page 13: So you think you know JavaScript

© 2013 Apperson Labs

“Where are we going thunder?” - Brian Regan

•How JavaScript works (from 10,000 feet)

•Common mistakes

•Titanium gotchas (aka “the dark corners”)

Page 14: So you think you know JavaScript

© 2013 Apperson Labs

Objects

Page 15: So you think you know JavaScript

© 2013 Apperson Labs

Object usage and properties

false.toString(); // 'false'[1, 2, 3].toString(); // '1,2,3'

function Foo(){}Foo.bar = 1;Foo.bar; // 1

Page 16: So you think you know JavaScript

© 2013 Apperson Labs

Object usage and properties

2.toString(); // raises SyntaxError

2..toString(); // the second point is correctly recognized2 .toString(); // note the space left to the dot(2).toString(); // 2 is evaluated first

Page 17: So you think you know JavaScript

© 2013 Apperson Labs

Object usage and properties

var obj = {    bar: 1,    foo: 2,    baz: 3};obj.bar = undefined;obj.foo = null;delete obj.baz;

for(var i in obj) {    if (obj.hasOwnProperty(i)) {        console.log(i, '' + obj[i]);    }}

Page 18: So you think you know JavaScript

© 2013 Apperson Labs

function Foo() {    this.value = 42;}Foo.prototype = {    fun: function() {}};

function Bar() {}

// Set Bar's prototype to a new instance of FooBar.prototype = new Foo();Bar.prototype.foo = 'Hello World';

// Make sure to list Bar as the actual constructorBar.prototype.constructor = Bar;

var test = new Bar(); // create a new bar instance

JS Prototype (correct):

Page 19: So you think you know JavaScript

© 2013 Apperson Labs

Bar.prototype = Foo.prototype;

JS Prototype (incorrect):

String.prototype.yell() = function() { return this + 'yay!!!!';}

Not inheriting correctly...

Prototyping native objects...

Page 20: So you think you know JavaScript

© 2013 Apperson Labs

Functions

Page 21: So you think you know JavaScript

© 2013 Apperson Labs

Function declaration:

// Example 1var test = function() { return "yay!";}

// Example 2function TestTwo() { return "yay also!";}

console.log(test); // "yay"console.log(TestTwo); // "yay also!""

Page 22: So you think you know JavaScript

© 2013 Apperson Labs

Function declaration:

// Example 1console.log(test); // Errorvar test = function() { return "yay!";};

// Example 2console.log(TestTwo); // “yay also!”function TestTwo() { return "yay also!";}

Page 23: So you think you know JavaScript

© 2013 Apperson Labs

Closures

function Counter(start) {    var count = start;    return {        increment: function() {            count++;        },

        get: function() {            return count;        }    }}

var foo = Counter(4);foo.increment();foo.get(); // 5

Page 24: So you think you know JavaScript

© 2013 Apperson Labs

Closures (the reference issue)

for(var i = 0; i < 10; i++) {    setTimeout(function() {        console.log(i);      }, 1000);}

Page 25: So you think you know JavaScript

© 2013 Apperson Labs

Closures (the reference issue)

for(var i = 0; i < 10; i++) {    (function(e) {        setTimeout(function() {            console.log(e);          }, 1000);    })(i);}

Page 26: So you think you know JavaScript

© 2013 Apperson Labs

Constructorsfunction Foo() {    this.bla = 1;}

Foo.prototype.test = function() {    console.log(this.bla);};var test = new Foo();

function Blob() {    return false;}new Blob(); // an empty object, not what’s expected

function Test() {    this.value = 2;

    return {        fun: 1    };}new Test(); // the returned object

Page 27: So you think you know JavaScript

© 2013 Apperson Labs

Constructors (factories)function Bar() {    var value = 1;    return {        method: function() {            return value;        }    }}Bar.prototype = {    foo: function() {}};

new Bar();Bar();

Page 28: So you think you know JavaScript

© 2013 Apperson Labs

Constructors (dropping “new”)function Foo() {    var obj = {};    obj.value = 'blub';

    var private = 2;    obj.someMethod = function(value) {        this.value = value;    }

    obj.getPrivate = function() {        return private;    }    return obj;}

Page 29: So you think you know JavaScript

© 2013 Apperson Labs

Constructors (dropping “new”)

•It uses more memory since the created objects do not share the methods on a prototype.

•In order to inherit, the factory needs to copy all the methods from another object or put that object on the prototype of the new object.

•Dropping the prototype chain just because of a left out `new` keyword is contrary to the spirit of the language.

Page 30: So you think you know JavaScript

© 2013 Apperson Labs

CommonMistakes

Page 31: So you think you know JavaScript

© 2013 Apperson Labs

setTimeout();

var timer = setTimeout(function() {// more code here}, 100);

This might not do what you think...

•It is expensive as it creates a new context

•The timer is not accurate as JS engines only have a single thread, forcing asynchronous events to queue waiting for execution.

Page 32: So you think you know JavaScript

© 2013 Apperson Labs

USING_CAPS_FOR_CONSTANTS

var DELAY = 20; // This is a constant, don’€šÃ„ôt change

For a long time we had to do this...

That is messy and dangerous... we have better options now!

const DELAY = 20; // This is a real constant

Page 33: So you think you know JavaScript

© 2013 Apperson Labs

parseInt();

var testONE = parseInt('20'); // This returns 20

Page 34: So you think you know JavaScript

© 2013 Apperson Labs

parseInt();

var testONE = parseInt('20'); // This returns 20

var testTWO = parseInt('08'); // This returns 0

Page 35: So you think you know JavaScript

© 2013 Apperson Labs

parseInt();

var testONE = parseInt('20'); // This returns 20

var testTWO = parseInt('08'); // This returns 0

var testTHREE = parseInt('08', 10); // This returns 08

Page 36: So you think you know JavaScript

© 2013 Apperson Labs

Global Variables

var bar = 'JavaScript can be fun!';

function foo() { bar = 'with a few exceptions'; return bar;}

console.log(foo()); // 'with a few exceptions'console.log(bar); // 'with a few exceptions'

Page 37: So you think you know JavaScript

© 2013 Apperson Labs

Global Variables

var bar = 'JavaScript can be fun!';

function foo() { var bar = 'with a few exceptions'; return bar;}

console.log(foo()); // 'with a few exceptions'console.log(bar); // 'JavaScript can be fun!'

Page 38: So you think you know JavaScript

© 2013 Apperson Labs

Global Variables

var bar = 'JavaScript can be fun!';

function foo() { var cat = bar cat = cat + ' yay!'; return bar;}

console.log(foo()); // 'Javascript can be fun! yay!'console.log(bar); // 'Javascript can be fun!'

Page 39: So you think you know JavaScript

© 2013 Apperson Labs

Not using hasOwnProperty();

function Cat() { // some code}

Cat.prototype.stomach = [ ];Cat.prototype.eat = function(food) { for( var i in food ) { this.stomach.push(food[i]); }};

Page 40: So you think you know JavaScript

© 2013 Apperson Labs

Not using hasOwnProperty();

function Cat() { // some code}

Cat.prototype.stomach = [ ];Cat.prototype.eat = function(food) { for( var i in food ) { if(food.hasOwnProperty(i)) { this.stomach.push(food[i]); } }};

Page 41: So you think you know JavaScript

© 2013 Apperson Labs

Using fake getters and setters...

function Foo(){ var _value;

this.getValue = function(){ return _value; };

this.setValue = function(val){ _value = val; };}

Foo.prototype.test = function() {console.log('The value is:' + this.value); // nopeconsole.log('The value is:' + this.getValue()); // OK...

}

Page 42: So you think you know JavaScript

© 2013 Apperson Labs

True getter and setters!

function Foo(){ var _value;

Object.defineProperty(this, "value", { get : function(){ return _value; }, set : function(newValue){ _value = newValue; }, enumerable : true });}

Foo.prototype.test = function() {console.log('The value is:' + this.value); // Yep!

}

Reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

Page 43: So you think you know JavaScript

© 2013 Apperson Labs

Not returning `this` on non-getters

function test() { }

test.prototype.run = function() { console.log('Doing something...');};

test.prototype.sayHi = function() { console.log('Doing something else...');};

var bar = new test();bar.run();bar.sayHi();

Page 44: So you think you know JavaScript

© 2013 Apperson Labs

Not returning `this` on non-getters

function test() { }

test.prototype.run = function() { console.log('Doing something...'); return this;};

test.prototype.sayHi = function() { console.log('Doing something else...'); return this;};

var bar = new test().run().sayHi();

Page 45: So you think you know JavaScript

© 2013 Apperson Labs

Try / Catch - A good idea?

function test() { try { // do stuff here } catch(e) { //this will blow up sometimes and I don’t know why... }}

Page 46: So you think you know JavaScript

© 2013 Apperson Labs

Try / Catch...

function test() { try { // do stuff here } catch() { // this might work, and it might blow up.// I have no idea why it blows up sometimes }}

Page 47: So you think you know JavaScript

© 2013 Apperson Labs

Try / Catch...

function test() { try { // do stuff here } catch(e) { // this worksconsole.log(e); }}

// There is a performance hit, yet it is responsible. So weigh the choice to use it carefully

Page 48: So you think you know JavaScript

© 2013 Apperson Labs

Titanium gotchas...

Reference - http://developer.appcelerator.com/blog/2012/02/what-is-a-titanium-proxy-object.html

•Objects returned by the Titanium API are not true JS objects[TIMOB-5818]

•Common JS does not work the same on both platforms[TIMOB-8082]

•Multiple JS engines to consider

Page 49: So you think you know JavaScript

© 2013 Apperson Labs

BETA

Page 50: So you think you know JavaScript

© 2013 Apperson Labs

Q & A

Page 51: So you think you know JavaScript

© 2013 Apperson Labs

Matt [email protected]

http://appersonlabs.com

twitter: @appersonlabs