javascript styles and some tips

46
JavaScript styles and some tips

Upload: vitor-baptista

Post on 09-May-2015

226 views

Category:

Technology


0 download

DESCRIPTION

by Filipe Sabella

TRANSCRIPT

Page 1: Javascript Styles and some tips

JavaScript stylesand some tips

Page 2: Javascript Styles and some tips

What you usually see around the Internet

Page 3: Javascript Styles and some tips

function Auction(initialPrice) {this.bids = [];this.initialPrice = initialPrice;this.currentPrice = initialPrice;

} Auction.prototype.bid = function (amount) {

this.currentPrice += amount;this.bids.push(amount);

}; Action.prototype.history = function () {

return this.bids.copy();}; var auction = new Auction(10.0); auction.bid(9001);console.log(auction.history());

Page 4: Javascript Styles and some tips

Pros

Page 5: Javascript Styles and some tips

Somewhat organized

Page 6: Javascript Styles and some tips

Kinda looks like a Class

Page 7: Javascript Styles and some tips

Allows changing behavior of existing instances

through prototype(but that's actually a bad thing)

Page 8: Javascript Styles and some tips

Cons

Page 9: Javascript Styles and some tips

Where's my scope?

Page 10: Javascript Styles and some tips

Everything's public

Page 11: Javascript Styles and some tips

A better approach

Page 12: Javascript Styles and some tips

function Auction(initialPrice) {var currentPrice = initialPrice;var bids = [];

this.bid = function (amount) {

currentPrice += amount;bids.push(amount);

};

this.history = function () {return bids.copy();

};} var auction = new Auction(10.0); auction.bid(9001);console.log(auction.history());

Page 13: Javascript Styles and some tips

Pros

Page 14: Javascript Styles and some tips

Everything contained in a single Function

Page 15: Javascript Styles and some tips

Proper scoping

Page 16: Javascript Styles and some tips

Control over public interface

Page 17: Javascript Styles and some tips

Cons

Page 18: Javascript Styles and some tips

"this" keyword is scary.apply, .call

Page 19: Javascript Styles and some tips

How to fix?

Page 20: Javascript Styles and some tips

function Auction(initialPrice) {var currentPrice = initialPrice;var bids = [];

var api = {};api.bid = function (amount) {

currentPrice += amount;bids.push(amount);

};api.history = function () {

return bids.copy();};

return api;

} var auction = new Auction(10.0); auction.bid(9001);console.log(auction.history());

Page 21: Javascript Styles and some tips

Pros

Page 22: Javascript Styles and some tips

No more "this"

Page 23: Javascript Styles and some tips

Cons

Page 24: Javascript Styles and some tips

Memory usage

Page 25: Javascript Styles and some tips

"prototype" won't work(but that's actually good)

Page 26: Javascript Styles and some tips

Tips

Page 27: Javascript Styles and some tips

Namespacing

Page 28: Javascript Styles and some tips

var ove = ove || {};ove.model = ove.model || {}; ove.model.Auction = function (...) {

// ...}; var auction = new ove.model.Action();

Page 29: Javascript Styles and some tips

There are libraries for thatbut it's nice to understand what's happening

Page 30: Javascript Styles and some tips

Monkey-patching Array, Function, and String's

finedon't mess with the DOM though

Page 31: Javascript Styles and some tips

Function.prototype.curry = function () { ... }; String.prototype.each = function () { ... }; Array.prototype.foldLeft = function () { ... };

Page 32: Javascript Styles and some tips

Know when you see this?

Page 33: Javascript Styles and some tips

people = [Person.new(18), Person.new(22), Person.new(65)]; canDrink = [];for person in people

if (person.age > 20)canDrink.push(person);

endend

Page 34: Javascript Styles and some tips

Why are you writing your Java in my Ruby?

Page 35: Javascript Styles and some tips

What about this?

Page 36: Javascript Styles and some tips

class Animal constructor: (@name) -> move: (meters) -> alert @name + " moved #{meters}m." class Snake extends Animal move: -> alert "Slithering..." super 5 class Horse extends Animal move: -> alert "Galloping..." super 45 sam = new Snake "Sammy the Python"tom = new Horse "Tommy the Palomino" sam.move()tom.move()

Page 37: Javascript Styles and some tips

Why are you writing your Ruby in my JavaScript?

Page 38: Javascript Styles and some tips

Moving on

Page 39: Javascript Styles and some tips

Avoid small magics

Page 40: Javascript Styles and some tips

<input id="field-id" /><input id="field-name" /><input id="field-email" /> function validate(field) {

var val = $('#field-' + field).val();// ...

} validate('id');validate('name');validate('email');

Page 41: Javascript Styles and some tips

Use the scope

Page 42: Javascript Styles and some tips

images.each(function (image) {var li = $('<li></li>', {

'class': 'image','data-id': image.id()});

container.append(li);}); <ul>

<li class="image" data-id="1"></li></ul> $('li.image', container).click(function () {

var id = this.attr('data-id');view.dialog.openImage(id);

});

Page 43: Javascript Styles and some tips

images.each(function (image) { var li = $('<li></li>') li.click(function () { view.dialog.openImage(image.id()); }); container.append(li);});

Page 44: Javascript Styles and some tips

Avoid state

Page 45: Javascript Styles and some tips

<ul id="cars"><li class="car">

<input type="checkbox">First Car</input></li><li class="car">

<input type="checkbox">Carman</input></li>

<ul> var selectedCars = [];$('#cars .car input').click(function () {

if (this.checked)selectedCars.push(this);

elseselectedCars.remove(this);

});

Page 46: Javascript Styles and some tips

$('#cars .car input:checked');