JacaScript patterns talk, Zohar Arad

Get Started. It's Free
or sign up with your email address
Rocket clouds
JacaScript patterns talk, Zohar Arad by Mind Map: JacaScript patterns talk, Zohar Arad

1. Delegation

1.1. Why

1.1.1. We don't need inheritance chain to borrow methods from objects

1.1.2. We can run object methods in different scopes

1.1.3. Mixins are way to do this

1.1.3.1. But we don't want to dirty JS namespace

1.1.4. Motivation

1.1.4.1. Native JS objects have useful methods

1.1.4.2. We can emulate desired methods using native methods

1.1.4.3. More expressive & shorter code

1.2. Examples

1.2.1. [].shift.call(str)

1.2.1.1. Get last char of a string

1.2.1.2. Works because both array& string have length method

1.2.1.3. You can apply array methods on Everything having a length method

1.2.2. "".trim.apply([" a", "b "]).split(",")

2. Slides

2.1. Http://goo.gl/kQk2D

3. Patterns

3.1. Mixins

3.1.1. Forcing OOP patterns on JS is wrong

3.1.1.1. It's power lies in prototypes

3.1.2. Zohar stopped using OO

3.1.2.1. After noticing that besides extending MooTools classes, there's usually nothing in his classes

3.1.3. Why mixins?

3.1.3.1. Code reuse

3.1.3.2. Modularity

3.1.3.3. Avoiding inheritance chain

3.1.3.4. Readability

3.1.3.5. Maintainability

3.1.4. Ruby has extremely useful notion

3.1.4.1. Extend

3.1.4.1.1. Add functionality to class

3.1.4.2. Include

3.1.4.2.1. Add functionality to instance

3.1.5. Example

3.1.5.1. See fiddle link in slides

3.1.5.1.1. Zohar's version is my modified version of Osmani's version

3.1.5.1.2. extend & include methods

3.1.5.1.3. Logger mixin

3.2. Pub/Sub

3.2.1. Why

3.2.1.1. Working synchronously in JS sucks

3.2.1.1.1. Hard-coded API

3.2.1.1.2. Easier to break

3.2.1.1.3. Less expressive

3.2.2. JS has a notion of events, lets use it

3.2.2.1. Instead of defining API we define messages

3.2.2.2. Action handling is encapsulated& distributed

3.2.2.2.1. Logic of handling is encapsulatedat the component listening to the event

3.2.2.2.2. Nice example is handling google Analytics events

3.2.2.2.3. Decoupling

3.2.2.2.4. More expressive code

3.2.2.3. Problems

3.2.2.3.1. We need to design our channels carefully

3.2.2.3.2. Avoid callback spaghetti

3.2.2.3.3. There's no delivery guarantee

3.2.3. See fiddle example in slides

3.2.3.1. subscribe to topic method

3.2.3.1.1. Returns token for easy unsubscribe

3.2.3.2. Publish method

3.2.3.2.1. To topic

3.2.3.3. Unsubscribe

3.2.3.4. Problem

3.2.3.4.1. All subscribers receive drive events

3.2.3.4.2. Error in callback will stop event handling - add try/catch around callback

4. Intro

4.1. Addi Osmani has a book & site with many patterns

4.2. There are many

5. Disclaimer

5.1. Not the Java OOP patterns