未加星标

A Concise Introduction to JavaScript Design Patterns

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

javascript design patterns are, essentially, efficient problem solving strategies. When you build large scale web applications of high complexity, code becomes increasingly difficult to manage and various problems arise throughout the building process. JavaScript design patterns not only provide solutions to these problems but also organize your code in a way that is easy to understand, maintain, scale and share. Design patterns also significantly reduce the size of code files and increase the speed of debugging your application.

This article serves as an introduction to JavaScript design patterns and therefore the explanations and examples are kept simple. For further information and advanced examples please visit the resources provided at the end of the article.

Certain popular patterns have developed over time and are used frequently due to their track record of providing viable solutions in the past. Still, new patterns are developed to overcome new challenges that arise. Following is an introduction to the three categories of JavaScript patterns and examples of each type.

Design Pattern Categories

JavaScript is an object oriented language and the types of JavaScript design patterns mainly revolve around handling objects. JavaScript design patterns are categorized into three different types, namely creational, structural and behavioral. They are used in object creation, overseeing relationships between different objects and organizing object communication. This article illustrates two patterns from each type. Note that there are many more design patterns not covered here.

Creational Creational design patterns, as the name indicates, are used during object creation. Not all objects are created using this kind of pattern, but they are very suitable for large scale applications. Both creational patterns shown below use the concept of inheritance. Inheritance is a vital characteristic of object oriented programming and essentially means taking something old and adding a new functionality to it, or changing it in some way.

Prototype The prototype design pattern allows you to create an object without actually creating an object. How is this done? Rather than creating an object from scratch, a new object is created by cloning an existing object. The clone inherits all the properties and methods of the original. However, the clone can redefine or override any property or method it wants. This is all done using prototypes. Every object in JavaScript has a prototype. Objects can inherit properties from their prototype. A prototype can also inherit properties from other object’s prototypes. Because of the way they work, prototypes make it possible to clone one object from another. Consider the code below //An object Dog is created var Dog= { numEyes= 2, numPaws= 4, numTail= 1, typeBarks= 3 }; //Object.create() is used to instantiate a new dog var yorkie = Object.create(Dog); //Change a property yorkie.typeBarks= 4;

The yorkie object is cloned from the Dog object, meaning yorkie inherits all the properties and methods of dog. However, yorkie is not limited to the properties of dog, and can now define its own properties that are not available in the dog object. Yorkie can also change the properties it wants such as redefining typeBarks as shown above.

Mixin- Mixins act how they sound. They allow you to “mix in” functionalities to other objects. Mixins make it possible to re-use functions throughout your code without having to define the function each time. The code below demonstrates how mixins work. // A mixin constructor with two functions var mixin = { flyUp: function(){}, flyDown: function(){} }; // An airplane constructor function airplane(){ startEngine: function(){}; }; // A bird constructor function bird(){ stopChirping: function(){}; }; //Extend statements _.extend(airplane.prototype, mixin); _.exend(bird.prototype, mixin); //Declaring new instances var deltaPlane = new airplane(); var blueJay= new bird(); //Using the functionalities of mixin deltaPlane.flyUp(); blueJay.flyDown();

The example above contains three constructors. There is a constructor for a mixin, an airplane, and a bird. The mixin constructor defines two functions, namely flyUp and flyDown. The airplane and bird constructors have their own functions but can inherit the functions of mixin using the extend statement. Then new instances of airplane and bird can be created which can now use the functions flyUp and flyDown from the mixin constructor.

Structural- Structural design patterns oversee the relationship between objects. One of the main goals of structural design is to ensure that if one part of the program changes, the other parts are not affected by the change and stay the same.

Module object oriented programming languages like JavaScript don’t have classes. One of the main advantages of classes is the option of private and public scope. The module pattern mimics this functionality and lets JavaScript objects behave like classes in this regard. Methods and variables can be made public or private within an object. Privacy is achieved using closures. Whatever is inside a closure is private and therefore protected from being accessed by other parts of the program. The following example demonstrates the use of closures. //Self-contained module var countModule = (function(){ var myCount=0; return{ decrementCount: function(){ return myCount--; } } })(); //Using the counter countModule.decrementCount();

In the code above countModule is a self-contained module. The variable myCount only exists within the closures of countModule and cannot be accessed from anywhere else. Neither can the function decrementCount be called from any other part of the program as it does not have global scope. The function decrementCount that is inside the module can access the variable myCount. The variables and functions inside countModule have private scope and cannot be changed by anything outside of it. This is how privacy is established in JavaScript. Notice that there are no explicit declarations of public or private as seen in classes of other languages.

Faade the faade pattern is aptly named because it produces a faade to users by hiding the inner workings of itself. The public is presented with a simple interface while the complex methods and functionalities are hidden. JavaScript libraries such as jQuery are easy to use because they utilize the faade pattern. Without facades, using some libraries would be more complicated and time consuming. The animate() and css() methods in jQuery are examples of facades.

Behavioral- Behavioral design handles the communication between objects. It is important that the communication between objects is organized, otherwise problems could arise that result in errors that are difficult to find.

Observer this design pattern involves two components. The subject and a list of observers. Whenever a change occurs in the subject, the list of observers

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaJavaScriptjQuery
分页:12
转载请注明
本文标题:A Concise Introduction to JavaScript Design Patterns
本站链接:http://www.codesec.net/view/482576.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(17)