JacaScript patterns talk, Zohar Arad

Get Started. It's Free
or sign up with your email address
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 But we don't want to dirty JS namespace

1.1.4. Motivation Native JS objects have useful methods We can emulate desired methods using native methods More expressive & shorter code

1.2. Examples

1.2.1. [].shift.call(str) Get last char of a string Works because both array& string have length method 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 It's power lies in prototypes

3.1.2. Zohar stopped using OO After noticing that besides extending MooTools classes, there's usually nothing in his classes

3.1.3. Why mixins? Code reuse Modularity Avoiding inheritance chain Readability Maintainability

3.1.4. Ruby has extremely useful notion Extend Add functionality to class Include Add functionality to instance

3.1.5. Example See fiddle link in slides Zohar's version is my modified version of Osmani's version extend & include methods Logger mixin

3.2. Pub/Sub

3.2.1. Why Working synchronously in JS sucks Hard-coded API Easier to break Less expressive

3.2.2. JS has a notion of events, lets use it Instead of defining API we define messages Action handling is encapsulated& distributed Logic of handling is encapsulatedat the component listening to the event Nice example is handling google Analytics events Decoupling More expressive code Problems We need to design our channels carefully Avoid callback spaghetti There's no delivery guarantee

3.2.3. See fiddle example in slides subscribe to topic method Returns token for easy unsubscribe Publish method To topic Unsubscribe Problem All subscribers receive drive events 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