Showing posts with label Patterns. Show all posts
Showing posts with label Patterns. Show all posts

Friday, January 17, 2014

JS: the right way

A post by William Oliveira@http://jstherightway.org/ 


CHOOSE YOUR PATH

GETTING STARTED

ABOUT

Created by Netscape in 1995 as an extension of HTML for Netscape Navigator 2.0, JavaScript had as its main function the manipulation of HTML documents and form validation. Before winning this name so famous nowadays, JavaScript was called Mocha, after LiveScript and finally, when it was released by Sun Microsystems, was baptized with the name by which he is known today. Because of the similar names, people confuses JavaScript with Java. Although both have the lexical structure of programming, they are not the same language. Different from C, C# and Java, JavaScript is an interpreted language. It means that it needs an "interpreter". In case of JavaScript, the interpreter is the browser.

CURRENT VERSION

The JavaScript standard is ECMAScript. As of 2012, all modern browsers fully support ECMAScript 5.1. Older browsers support at least ECMAScript 3. A 6th major revision of the standard is being worked on.
A good reference to versions, references and news about JavaScript, you can find at Mozilla Developer Network.

THE DOM

The Document Object Model (DOM) is an API for HTML and XML documents. It provides a structural representation of the document, enabling you to modify its content and visual presentation by using a scripting language such as JavaScript. See more at Mozilla Developer Network - DOM.

JS CODE STYLE

CONVENTIONS

As every language, JavaScript has many code style guides. Maybe the most used and recommended is the Google Code Style Guide for JavaScript, but we recommend you to read the Idiomatic.js.

LINTING

Nowadays the best tool to linting your JS code is the JSHint. We recommend you whenever possible to verify you code style and patterns with a Lint tool.

THE GOOD PARTS

OBJECT ORIENTED

JavaScript has strong object-oriented programming capabilities, even though some debates have taken place due to the differences in object-oriented JavaScript compared to other languages.

ANONYMOUS FUNCTIONS

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

FUNCTIONS AS FIRST-CLASS OBJECTS

Functions in javascript are first class objects. This means that javascript functions are just a special type of object that can do all the things that regular objects can do.

LOOSE TYPING

For many front end developers, JavaScript was their first taste of a scripting and/or interpretive language. To these developers, the concept and implications of loosely typed variables may be second nature. However, the explosive growth in the demand for Web 2.0-ish applications has resulted in a growing number of back end developers that have had to dip their feet into pool of client side technologies. Many of these developers are coming from a background in strongly typed languages, such as C# and Java, and are unfamiliar with both the freedom and the potential pitfalls involved in working with loosely typed variables.

PATTERNS

DESCRIPTION

Too many of the classical design patterns are implemented and have uses in JavaScript and there are some JS Patterns that are exclusive.
A good way to learn about these is Addy Osmani’s open source book Learning JavaScript Design Patterns, and the links below are (in the majority) based on it.

DESIGN PATTERNS

Creational Design Patterns
Structural Design Patterns
Behavioral Design Patterns

MV* PATTERNS

There are some implementations of the traditional MVC Pattern and its variations in JS

TESTING TOOLS

DESCRIPTION

Various libraries and frameworks to do tests in JavaScript.

LINKS

Created by TJ Holowaychuk
Created by jQuery
Created by Pivotal Labs
Created by John Resig
Created by Choan Gálvez
Created by the team behing AngularJS. Mostly by Vojta Jina
Created by Sitepen

FRAMEWORKS

CLIENT-SIDE

  • jQuery
  • jQuery is a fast, small, and feature-rich JavaScript library. Built by John Resig.
  • Backbone.js
  • Very popular JS client-site framework, built by @jashkenas.
  • Ember.js
  • Built by @wycats, jQuery and Ruby on Rails core developer.
  • Knockout.js
  • Simplify dynamic JavaScript UIs by applying the Model-View-View Model (MVVM).
  • Angular.js
  • Built by Google, Angular.js is like a polyfill for the future of HTML
  • Cappuccino
  • Cappuccino is an open source framework that makes it easy to build desktop-caliber applications that run in a web browser.
  • JavaScript MVC
  • JavaScriptMVC is an open-source framework containing the best ideas in jQuery development.
  • YUI
  • Built by Yahoo!, YUI is a free, open source JavaScript and CSS library for building richly interactive web applications.
  • ZeptoJS
  • Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you use jQuery, you already know how to use Zepto.
  • Dojo Toolkit
  • Dojo is a free, open source JavaScript toolkit for building high performance web applications. Project sponsors include IBM and SitePen.

    READING

    ARTICLES

  • Understanding JavaScript OOP
  • Understanding “Prototypes” in JavaScript
  • Understanding JavaScript Function Invocation and “this”
  • Partial Application in JavaScript
  • BOOKS

  • JavaScript: The Definitive Guide
  • JavaScript: The Good Parts
  • Test-Driven JavaScript Development
  • JavaScript Patterns
  • High Performance JavaScript
  • Eloquent JavaScript
  • FREE E-BOOKS

    PORTALS

    Wednesday, January 1, 2014

    JavaScript Patterns Collection

    A post by Shi Chuan@http://shichuan.github.io/javascript-patterns/



  • General Patterns
  • jQuery Patterns
  • jQuery Plugin Patterns
  • Literals and Constructor Patterns
  • Function Patterns
  • Object Creation Patterns
  • Code Reuse Patterns
  • Design Patterns

  • - See more at: http://shichuan.github.io/javascript-patterns/#sthash.umQPoMlW.dpuf
    A JavaScript pattern and antipattern collection that covers function patterns, jQuery patterns, jQuery plugin patterns, design patterns, general patterns, literals and constructor patterns, object creation patterns, code reuse patterns, DOM and browser patterns (upcoming).
    Patterns collected while developing 喜感网.

    General Patterns

    jQuery Patterns

    • requery - avoid requery by using jQuery chaining
    • append - use string concatenate and set innerHTML
    • data - pattern and antipattern of using data
    • context and find - use find over context
    • detach - take element off the DOM while manipulating them
    • event delegation - event delegation pattern and antipattern
    • selector cache - using selector cache to avoid requery
    • window scroll event - avoid attaching handlers to the window scroll event

    Selector

    Publish–subscribe

    • Method 1 - custom events using .on() and .off()
    • Method 2 - using jQuery 1.7's $.Callbacks() feature
    • Method 3 - using jQuery UI $.Observable
    • Method 4 - third-party plugins

    jQuery Plugin Patterns

    Literals and Constructors Patterns

    • Object literal - use the simpler and reliable object literal instead of new Object();
    • Enforcing new - when you forget `new`, `this` inside the constructor will point to the global object
    • Array literal - use array literal notation to avoid potential errors when creating dynamic arrays at runtime
    • Working with JSON - use library from JSON.org, YUI, jQuery instead of eval for parsing
    • Primitive wrappers - try to use the primitive without wrapper
    • Regular expression literal - try to use the shorter literal notation

    Function Patterns

    API Patterns

    • Callback patterns - when you pass function A to function B as a parameter, function A is a callback function
    • Configuration objects - keep control of function arguments and makes it easily configurable
    • Returning functions - one function returns another function or create another function on-demand
    • Currying - used to create new functions dynamically by partially applying a set of arguments
    • Partial application - the process of fixing a number of arguments to a function, producing another function of smaller arity

    Initialization patterns

    Performance patterns

    • Memoization - use function properties to avoid repeated computation
    • Self-defining functions - self-overwrite with new bodies to do less work from the second invocation and after

    Object Creation Patterns

    • Namespace - namespaces help reduce the number of globals required and avoid naming collisions or excessive name prefixing
    • Declaring Dependencies - it's good to declare the modules your code relies on at the top
    • Private Properties and Methods - JavaScript doesn't have special syntax for private members, you can implement them using a closure
    • Revelation Pattern - it is about having private methods, which you also expose as public methods
    • Module Pattern - all the methods are kept private and you only expose those that you decide at the end
    • Sandbox - it provides an environment for the modules to work without affecting other modules and their personal sandboxes
    • Static Members - public and private static members
    • Object Constants - an implementation of a contant object provides set, inDefined and get methods
    • Chaining Pattern - it enables you to call methods on an object one after the other
    • method() Method - adding convenient funcationality to a language

    Code Reuse Patterns

    Classical Patterns (patterns that should generally be avoided)

    • The default pattern - create an object using the Parent() constructor and assign this object to the Child()'s prototype
    • Rent a constructor - it borrows the parent constructor, passing the child object to be bound to this and also forwarding any arguments
    • Rent and Set Prototype - restricts object creation for a class to only one instance
    • Share the Prototype - restricts object creation for a class to only one instance
    • A Temporary Constructor - first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
    • Klass - generally a pattern that should be avoided unless one is more comfortable with class than prototype

    Preferred Patterns

    Design Patterns

    Creational

    • Builder - constructs complex objects by separating construction and representation
    • Factory method - creates objects without specifying the exact class to create
    • Singleton - restricts object creation for a class to only one instance

    Structural

    • Decorator - dynamically adds/overrides behaviour in an existing method of an object
    • 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

    Behavioral

    • Chain of responsibility - delegates commands to a chain of processing objects
    • Command - creates objects which encapsulate actions and parameters
    • Iterator - implements a specialized language
    • 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 of observer objects to see an event
    • Strategy - allows one of a family of algorithms to be selected on-the-fly at runtime

    References

    1. The Essentials of Writing High Quality JavaScript
      http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
    2. JSPatterns
      http://www.jspatterns.com/
    3. jQuery Anti-Patterns for Performance & Compression
      http://paulirish.com/2009/perf/
    4. How DRY Affects JavaScript Performance
      http://velocityconf.com/velocityeu/public/schedule/detail/21634
    5. Object Oriented JavaScript
      http://www.packtpub.com/object-oriented-javascript/book
    6. JavaScript Patterns
      http://shop.oreilly.com/product/9780596806767.do
    7. JavaScript: The Good Parts
      http://shop.oreilly.com/product/9780596517748.do
    8. Pro JavaScript Design Patterns
      http://jsdesignpatterns.com/
    9. Essential JavaScript Design Patterns For Beginners, Volume 1.
      http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/
    10. Eloquent JavaScript
      http://eloquentjavascript.net/
    - See more at: http://shichuan.github.io/javascript-patterns/#sthash.umQPoMlW.dpuf