24
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission. Keith Donald - SpringSource Modern JavaScript

SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Keith Donald - SpringSource

Modern JavaScript

Page 2: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Agenda

• A look at Modern JavaScript Application Engineering– Core Language– Frameworks

• Q & A

• Goal of this talk– Help you with developing and structuring large JavaScript

applications

2

Page 3: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Introduction

• Traditionally, JavaScript (JS) has gotten a bad wrap– a toy for script-kiddies– a cut & paste ghetto

• “Hell is other peopleʼs JavaScript”– Some guy on Twitter

3

Page 4: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Introduction (2)

• Yet, JS is not a toy, it is– An expressive, powerful language– Everywhere– Performant

• JS is worth your time to master

4

Page 5: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Core Language and Patterns

Modern JS

5

Page 6: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

• Functions are first-class• Everything is an object• Closures• Prototypes

6

Key JavaScript Concepts

Page 7: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Functions are first class

7

var sayHello = function(name) { return "hello " + name;}

function logResult(fx, arg) { console.log(fx.call(null, arg));}

logResult(sayHello, "keith");

["roy", "craig"].forEach(function(name) { logResult(sayHello, name);});

Page 8: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Everything is an object

8

var obj = {};console.log(obj instanceof Object);console.log([] instanceof Object);console.log((function() {}) instanceof Object);

obj.property = "foo";obj.method = function() { console.log(this); return "bar";}console.log(obj.method());

Page 9: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Closures

9

var sayHelloMaker = function(name) { return function() { return "hello " + name; }}var helloKeith = sayHelloMaker(“keith”), helloRoy = sayHelloMaker(“roy”);console.log(helloKeith());console.log(helloRoy());

(function() { var private = “value”; return { public: function() { console.log(“Invoked a public function that can access private data!”); } };})();

Page 10: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Prototypes

10

var obj = {}, obj2 = Object.create(Object.prototype); // equivalent

var user = (function() { function encode(password) { ... } // private return { authenticate: function(password) { return encode(password) === this.encodedPassword; } toString: function() { return this.username }; // overrides Object.toString(); }})();var keith = Object.create(user, { username: { value: “kdonald”, enumerable: true } , encodedPassword: { value: “abfdfca234598b721” }});console.log(keith);console.log(keith.authenticate(“melbourne”));

Page 11: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Notes about this

11

var fx = function() { console.log(this);}fx(); // this === window

var obj = {};obj.property = fx;obj.property(); // this === obj

jQuery(“#myDiv”).click(fx); // on click, this === myDiv

// key Function primitives - ʻobjʼ becomes ʻthisʼ when ʻfxʼ is invokedfx.call(obj, arg1, arg2, ...);fx.apply(obj, [args]);

Page 12: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

RewardNetwork: Reference App

12

Page 13: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

RewardNetwork.java

13

public class RewardNetwork {

public RewardConfirmation rewardForPurchase(Purchase p) { Account account = accounts.withCreditCard(p.getCreditCard()); Merchant merchant = merchants.withId(p.getMerchantId()); Money reward = merchant.calculateReward(purchase, account); account.distribute(reward); accounts.saveChanges(account); return rewards.log(reward, account, purchase); }

}

Page 14: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Usage Example

14

public class Main {

public static void main(String[] args) { RewardNetwork rewardNetwork = new RewardNetwork(); rewardNetwork.setAccounts(accounts); rewardNetwork.setMerchants(merchants); Purchase p = new Purchase(new Money(“30.00”), “bizzarros”, new CreditCard(“1234123412341234”, “2/2013”)); RewardConfirmation confirmation = rewardNetwork.rewardForPurchase(p); } }

Page 15: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

How to implement in JavaScript?

• With idiomatic JS

• Not JS that tries to emulate Java

15

Page 16: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

rewardnetwork.js

16

define(["accounts", "merchants", "rewards"], function(accounts, merchants, rewards) { return { rewardForPurchase: function(purchase) { var account = accounts.withCreditCard(purchase.creditCard); var merchant = merchants.withId(purchase.merchantId); var reward = merchant.calculateReward(purchase, account); account.distribute(reward); accounts.saveChanges(account); return rewards.log(reward, account, purchase); } };});

Page 17: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

main.js

17

require(["rewardnetwork"], function(rewardnetwork) { var confirmation = rewardnetwork.rewardForPurchase({ amount: 30.00, merchantId: “bizzarros”, creditCard: { number: “1234123412341234”, expiration: “02/2013” } });});

Page 18: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Demo

RewardNetwork JS

18

Page 19: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

Summary

• Learn the key concepts– Function are first-class– Everything is an object– Closures– Prototypes

• Favor idiomatic JS

• Use modules to define units of JS code that– Have dependencies– Encapsulate private data and behavior– Export public behavior

19

Page 20: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Frameworks

Modern JS

20

Page 21: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

General Categories

• DOM and Ajax– jQuery, Zepto

• Client Side Templating– Handlebars, Mustache, Eco

• MVC– Backbone, AgilityJS

• Module (or Script) Loaders– RequireJS

• Utilities– Underscore

• Full-Stack– Dojo, SproutCore

21

Page 22: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Demo: Frameworks

Modern JS

22

Page 23: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

People and Resources

• Brendan Eich• Douglas Crockford• Alex Russell• John Resig• Jeremy Ashkenas• Ryan Dahl• Yehuda Katz• Rebecca Murphy• Brian Leroux

• JavaScript Weekly, Hacker News

23

Page 24: SpringSource 2GX 2009...SpringSource 2GX 2009 Author Adam FitzGerald Created Date 10/25/2011 7:19:48 AM

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Q&A

24