background preloader

Design patterns

Facebook Twitter

Designing Better JavaScript APIs. Advertisement At some point or another, you will find yourself writing JavaScript code that exceeds the couple of lines from a jQuery plugin.

Designing Better JavaScript APIs

Your code will do a whole lot of things; it will (ideally) be used by many people who will approach your code differently. They have different needs, knowledge and expectations. This article covers the most important things that you will need to consider before and while writing your own utilities and libraries. We’ll focus on how to make your code accessible to other developers. Peter Drucker once said: “The computer is a moron.” Table of Contents Fluent Interface The Fluent Interface1 is often referred to as Method Chaining (although that’s only half the truth). Aside from major simplifications, jQuery offered to even out severe browser differences.

Method Chaining The general idea of Method Chaining6 is to achieve code that is as fluently readable as possible and thus quicker to understand. Command Query Separation Going Fluent Consistency Callbacks. Helpful JavaScript Patterns. So you write JavaScript.

Helpful JavaScript Patterns

That’s pretty much a given for today’s modern web apps. Unfortunately, JavaScript doesn’t always get the organization it deserves and ends up being a procedural mess of jQuery on ready statements. In this post, I’m going to show you two of my favorite patterns for keeping JavaScript well organized. But before I do, let’s go over a few prerequisites.

Namespace Your Code One of the worst and most common JavaScript mistakes is assigning variable onto the global namespace (aka ‘window’) if you’re running JavaScript in a browser. And avoid this example: Use Strict Mode Even inside a namespaced function, there can still be a problem. Alternatively, you can use strict mode. If you’re interested in learning more about strict mode, check out this article on John Resig’s blog. Lint your JavaScriptJSLint evaluates your JavaScript against Douglas Crockford’s coding suggestions. The main benefits of using JSLint are: OK! Pros: Cons: * You can only have one. But the drawbacks are: Essential JavaScript Design Patterns For Beginners. Design patterns are reusable solutions to commonly occurring problems in software design.

They are both exciting and a fascinating topic to explore in any programming language. One reason for this is that they help us build upon the combined experience of many developers that came before us and ensure we structure our code in an optimized way, meeting the needs of problems we're attempting to solve. Design patterns also provide us a common vocabulary to describe solutions.

This can be significantly simpler than describing syntax and semantics when we're attempting to convey a way of structuring a solution in code form to others. In this book we will explore applying both classical and modern design patterns to the JavaScript programming language. Target Audience This book is targeted at professional developers wishing to improve their knowledge of design patterns and how they can be applied to the JavaScript programming language.

Acknowledgments Credits Reading We already use patterns everyday. Understanding Design Patterns in JavaScript. Today, we're going to put on our computer science hats as we learn about some common design patterns.

Understanding Design Patterns in JavaScript

Design patterns offer developers ways to solve technical problems in a reusable and elegant way. Interested in becoming a better JavaScript developer? Then read on. Republished Tutorial Every few weeks, we revisit some of our reader's favorite posts from throughout the history of the site. Solid design patterns are the basic building block for maintainable software applications. A design pattern is a reusable software solution Simply put, a design pattern is a reusable software solution to a specific type of problem that occurs frequently when developing software. Patterns are proven solutions to software development problems patterns are scalable as they usually are structured and have rules that you should follow patterns are reusable for similar problems We'll get into some examples of design patterns further in to the tutorial.

Take a look at the three examples above.