Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are design patterns in JavaScript helpful? And what exactly are they?

I have been learning more and more javascript; it's a necessity at my job. We have a web application that uses a lot of javascript and I will be doing more and more every day. I have read bits and pieces about design patterns, but was wondering if someone could just give me a cut and dry example and definition. Are they something that would benefit me? Or is it more high level?

like image 613
jrutter Avatar asked Dec 14 '08 15:12

jrutter


People also ask

Are design patterns useful in JavaScript?

JavaScript modules are the most prevalently used design patterns for keeping particular pieces of code independent of other components. This provides loose coupling to support well-structured code. For those that are familiar with object-oriented languages, modules are JavaScript “classes”.

Are design patterns really useful?

Design patterns are a toolkit of tried and tested solutions to common problems in software design. Even if you never encounter these problems, knowing patterns is still useful because it teaches you how to solve all sorts of problems using principles of object-oriented design.

What are design patterns and how are they useful?

Design patterns are commonly defined as time-tested solutions to recurring design problems. The term refers to both the description of a solution that you can read and an instance of that solution as used to solve a particular problem.

What patterns do you know and successfully use in JavaScript?

There are three(3) types of patterns. Creational, structural, and behavioral. Creational — Addresses problems related to creating objects. Structural — Addresses the relationship between entities and how together they can compose a larger structure.


2 Answers

Design patterns are generic and usually elegant solutions to well-known programming problems. Without knowing what problem you're working in, I would say "Yes" they can help make your code more manageable.

This link and this link make some reference to design patterns in Javascript. They may be worth reviewing.

like image 186
Peter K. Avatar answered Nov 06 '22 13:11

Peter K.


One of the most practical and easy to use JavaScript-specific design pattern I've encountered is the Module Pattern, which is a modified Singleton pattern that "namespaces" related code and prevents the global scope from getting cluttered with variables and functions that might conflict with each other in a complicated page.

like image 38
Kevin Gorski Avatar answered Nov 06 '22 13:11

Kevin Gorski