18
JS Patterns How to improve javascript code

Mini-Training: Javascript Patterns

Embed Size (px)

DESCRIPTION

An introduction to design patterns used in Javascript

Citation preview

Page 1: Mini-Training: Javascript Patterns

JS Patterns

How to improve javascript code

Page 2: Mini-Training: Javascript Patterns

Reminder :

3 categories design patterns :

-> Creational-> Structural-> Behavioral

Antipattern :

-> Wrong design -> Result of bad habit, bad practice, bad idea…

Page 3: Mini-Training: Javascript Patterns

Best practice general :

Function Declarations : creating anonymous functions and assigning them to a variable

Implied Typecasing : avoid implied typecasting

Page 4: Mini-Training: Javascript Patterns

Avoid eval() : "Eval is evil."

Best practice general :

Minimize Global (Not clear & unexpected results) / Hoisting

Working with Json (JavaScript Object Notation)

Use literal notation ( array & Regex)

Use parseInt()

Page 5: Mini-Training: Javascript Patterns

Best practice – code reuse :

Use constructor / share prototype => use javascript object

Use functions :- Callback / Chaining Pattern - Configuration objects- Immediate functions (could be used to init object)

- Memoization : use function properties to avoid repeated computation

Page 6: Mini-Training: Javascript Patterns

Design pattern - Creation - best practice :

Use Namespace : namespaces help reduce the number of globals required and avoid naming collisions or excessive name prefixing

Use Module / Declare dependencies

Use private Properties / Methodes / Static Members / Object constants

Use sandbox : it provides an environment for the modules to work without affecting other modules and their personal sandboxes

Page 7: Mini-Training: Javascript Patterns

Design pattern – Creation :

Use Builder : constructs complex objects by separating construction and representation

Use Factory : creates objects without specifying the exact class to create

Page 8: Mini-Training: Javascript Patterns

Design pattern – Structural :

Use Decorator : dynamically adds/overrides behaviourin an existing method of an object

Page 9: Mini-Training: Javascript Patterns

Use Facade : provides a simplified interface to a large body of code

Proxy : provides a placeholder for another object to control access,reduce cost, and reduce complexity

Design pattern – Structural :

Page 10: Mini-Training: Javascript Patterns

Design pattern – Behavioral :

Chain of responsibility : delegates commands to a chain of processing objects

Command : creates objects which encapsulate actions and parameter

Mediator : allows loose coupling between classes by being the only class that has detailed knowledge of their methods

Observer : is a publish/subscribe pattern which allows a number ofobserver objects to see an event

Strategy : allows one of a family of algorithms to be selected on-the-fly at runtime

Page 11: Mini-Training: Javascript Patterns

jQuery :

requery : avoid requery by using jQuery chaining

append : use string concatenate and set innerHTML

append : use string concatenate and set innerHTML

detach : take element off the DOM while manipulating them

Page 12: Mini-Training: Javascript Patterns

jQuery : selector cache : using selector cache to avoid requery

selector Left and Right - specific on the right, light on the left

Selector Decending from id : be more specific

Use publish / subscribe : $.Callbacks() & $.Observable

Page 13: Mini-Training: Javascript Patterns

jQuery Plugin :

Basic : the most basic pattern

Extend : enables you to define multiple functions at once and which sometimes make more sense semanticall

Page 14: Mini-Training: Javascript Patterns

jQuery :

UI Widget factory : for building complex, stateful plugins based on object-oriented principles

Page 15: Mini-Training: Javascript Patterns

References :

http://shichuan.github.io/javascript-patterns/#general-patterns

JavaScript Patterns – O’REILLY

http://api.jquery.com/

http://www.codetails.com/2014/05/27/top-10-javascript-traps-for-a-c-developer/

Learning JavaScript Design Patterns – O’REILLY

Page 16: Mini-Training: Javascript Patterns

Find out more

• On https://techblog.betclicgroup.com/

Page 17: Mini-Training: Javascript Patterns

We want our brands to be easy to use

for every gamer around the world.

Join us to make that happen.

Everything we do reflect our values

Come and work in a friendly

atmosphere based on trust &

cooperation between IT Teams.

Learn & Share with us Friday tech trainings, BBL, Meetups, Coding Dojo, Innovation Day & more

If you want to contribute to the success of our group, look at all the challenges we

offer HERE

Want to be part of a great online gambling company?

Check out our Carreers accounton Stackoverflow

Page 18: Mini-Training: Javascript Patterns

About Us• Betclic Everest Group, one of the world leaders in online

gaming, has a unique portfolio comprising variouscomplementary international brands: Betclic, Everest, bet-at-home.com, Expekt, Monte-Carlo Casino…

• Through our brands, Betclic Everest Group places expertise,technological know-how and security at the heart of ourstrategy to deliver an on-line gaming offer attuned to thepassion of our players. We want our brands to be easy to usefor every gamer around the world. We’re building ourcompany to make that happen.

• Active in 100 countries with more than 12 million customersworldwide, the Group is committed to promoting secure andresponsible gaming and is a member of several internationalprofessional associations including the EGBA (EuropeanGaming and Betting Association) and the ESSA (EuropeanSports Security Association).